c++runtime

How to get C++ object name in run time?


Can I get an object's name in run time (like getting an object's type via RTTI)? I want the object to be able to print its name.


Solution

  • Its not possible. For on thing, an object doesn't have a unique name.

    A a;
    A& ar = a;  // both a and ar refer to the same object
    
    new A;  // the object created doesn't have a name
    
    A* ap = new A[100];  // either all 100 objects share the same name, or need to 
                         // know that they are part of an array.
    

    Your best bet is to add a string argument to the objects constructor, and give it a name when its created.