Sunday, August 2, 2009

How to get object name in c++?

hi ,





i am trying to get the object name, see the code below, but not able to find method, what could be the code in place of ???????.





#include "iostream"


using namespace std;


class A{


public:


int i;


void printObjectname(){


cout %26lt;%26lt; ????????;//%26lt;--what could be here, to print the object name as 'objectA'


}


};





int _tmain(int argc, _TCHAR* argv[])


{


A *objectA = new A();


objectA-%26gt;printObjectname();





return 0;


}

How to get object name in c++?
include the %26lt;typeinfo%26gt; header file and use function typeid()





http://publib.boulder.ibm.com/infocenter...
Reply:You have read the documentation of C++.
Reply:Sorry, there's no such thing in C++. You have to do this manually, i.e. change the code in every class:





virtual void printObjectname() { cout "A"; }





or even better:


virtual const char *getClassname() { return "A"; }





make the function virtual, then it can be overwritten in inherited classes.


No comments:

Post a Comment