how to get object name
-
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 << ????????;//<--what could be here, to print the object name as 'objectA' } }; int _tmain(int argc, _TCHAR* argv[]) { A *objectA = new A(); objectA->printObjectname(); return 0; } santosh
-
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 << ????????;//<--what could be here, to print the object name as 'objectA' } }; int _tmain(int argc, _TCHAR* argv[]) { A *objectA = new A(); objectA->printObjectname(); return 0; } santosh
Hi, this can not be done.
new A()
is an object, it could have a name.objectA
is not an object, it is a variable, it holds a reference to the class A object. There could be many more references to the same object, it suffices to doA* objectB=objectA;
, now how would the class A object have to know the name of all the variables pointing to it, and in particular the one you want?? Suggestion: give your objects a name by providing a Name property and/or a name argument in their constructor, sonew A("aha");
would create a new class A object and give it the name "aha". Now you can ask that object for its name, and/or you can override its ToString() method so it identifies itself. Remark: this works independently of the variable(s) that may refer to that object! :)Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets