Monday, April 5, 2010

How to get a variable name at runtime - C++

Different ways to get a variable name dynamically - C++

Where I need this :
For my application we need XML serialization. If we are using .Net it is very easy to serialize a class.

Solution 1:
Create a class which can hold variable name and value
Eg: MyClass a(“a”,1); but this is not automatic .

Solution 2:
#define GET_VARIABLE_NAME(var) #var

int _tmain(int argc, _TCHAR* argv[])
{
      char* strName = "Sen SD";
      printf("Variable name = %s",GET_VARIABLE_NAME(strName));
      int nPinNumber = 691301;
      printf("Variable name = %s",GET_VARIABLE_NAME(nPinNumber));
      return 0;
}

 

No comments: