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

前端 未结 8 1127
失恋的感觉
失恋的感觉 2021-01-17 11:08

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.

相关标签:
8条回答
  • 2021-01-17 11:52

    If you mean the name of the variable, I don't think this is possible. Maybe if you compile with the GNU Debugger option on ... but even in that way I don't think the language have constructs to do that.

    0 讨论(0)
  • 2021-01-17 11:54

    C++ doesn't really support reflection. However, a bit of googling produced a couple of alternate methods, I doubt you will find them useful though.

    0 讨论(0)
  • 2021-01-17 12:06

    The language does not give you access to that information.
    By the time the code has been compiled all named objects have been translated into relative memory locations. And even these locations overlap because of optimization (ie once a variable is no longer in use its space can be used by another variable).

    The information you need is stored in the debug symbols that are generated by most compilers but these are usually stripped from release versions of the executable so you can not guarantee they exist.

    Even if the debug symbols existed they are all compiler/platform specfic so your code would not be portable between OS or even compilers on the same OS. If you really want to follow this course you need to read and understand how the debugger for your platform works (unless you have already written a compiler this is very non trivial).

    0 讨论(0)
  • 2021-01-17 12:07

    C++ objects don't have 'names' (unless I am understanding the problem wrong) Your best hope is to name them as you make them.

    class NamedObject
    {
      String name;
      NamedObject(String argname)
      { 
        name = argname;
      }
    }
    
    NamedObject phil("phil");
    
    0 讨论(0)
  • 2021-01-17 12:08

    So, this is basically what I did. It's hacky, but it does the trick. I created a variadic macro that takes advantage of stringizing. Unfortunately, it becomes clumsy with the need for a _dummy parameter in order to provide the pseudo-default ctor, because you cannot omit the comma separating the named argument from the variable arguments (I even tried with gnu cpp, but was unsucessful--may I didn't try hard enough).

    #include <string>
    
    #define MyNamedClass( objname, ... ) MyClass objname(__VA_ARGS__, #objname )
    
    class MyClass
    {
    public:
       MyClass( void* _dummy=nullptr, const std::string& _name="anonymous") : name( _name ) {}
       MyClass( int i, const std::string& _name="anonymous" ) : name( _name ) {}
    
    private:
       std::string name;
    };
    
    
    int main()
    {
       MyClass mc0;
       MyClass mc1(54321);
       MyNamedClass( mc2, nullptr);
       MyNamedClass( mc3, 12345 );
    
       return 0;
    }
    
    0 讨论(0)
  • 2021-01-17 12:09

    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.

    0 讨论(0)
提交回复
热议问题