如何在运行时获取C ++对象名? [英] How to get C++ object name in run time?
本文介绍了如何在运行时获取C ++对象名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在运行时获取对象的名称(例如通过RTTI获取对象的类型)吗?我希望该对象能够打印其名称.
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.
推荐答案
不可能.对于事物,对象没有唯一的名称.
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 ++对象名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文