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

前端 未结 8 1230
失恋的感觉
失恋的感觉 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 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");
    

提交回复
热议问题