本文介绍了在运行时获取C ++对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在运行时获取对象的名称(如通过RTTI获取对象的类型)?我希望对象能够打印它的名称。
Can I get object's name in run time (like getting object's type via RTTI)? I want the object to be able to print its name.
谢谢。
推荐答案
它不可能。对于on,对象没有唯一的名称。
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.
这篇关于在运行时获取C ++对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!