如何获得 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于 C++ 中的对象没有任何名称,因此您无法获取它们。 唯一可以识别对象的就是它的地址。
否则,您可以实现您的命名方案(这意味着对象将具有一些
char*
或std::string
成员及其名称)。 您可以通过 Qt 的 QObject 层次结构来激发自己的灵感,它使用类似的方法。Since objects in C++ don't have any names, you cannot get them. The only thing you can get to identify an object is its address.
Otherwise, you can implement your naming scheme (which means the objects would have some
char*
orstd::string
member with their name). You can inspire yourself in Qt with their QObject hierarchy, which uses a similar approach.这是不可能的。 事实上,一个对象没有唯一的名称。
最好的办法是向对象构造函数添加一个字符串参数,并在创建时为其指定一个名称。
Its not possible. For on thing, an object doesn't have a unique name.
Your best bet is to add a string argument to the objects constructor, and give it a name when its created.
该语言不允许您访问该信息。
编译代码时,所有命名对象都已转换为相对内存位置。 甚至这些位置由于优化而重叠(即一旦一个变量不再使用,它的空间就可以被另一个变量使用)。
您需要的信息存储在大多数编译器生成的调试符号中,但这些信息通常从可执行文件的发行版本中删除,因此您不能保证它们存在。
即使调试符号存在,它们都是特定于编译器/平台的,因此您的代码不能在操作系统甚至同一操作系统上的编译器之间移植。 如果您确实想学习本课程,您需要阅读并了解您平台的调试器如何工作(除非您已经编写了编译器,这非常重要)。
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).
这可能是 GCC 特有的:
This may be GCC-specific:
C++ 并不真正支持反射。 然而,谷歌搜索产生了一些替代方法,我怀疑你但会发现它们很有用。
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.
C++ 对象没有“名称”(除非我错误地理解了问题),您最好的希望是在创建它们时为它们命名。
C++ objects don't have 'names' (unless I am understanding the problem wrong) Your best hope is to name them as you make them.
如果你的意思是变量的名称,我认为这是不可能的。 也许如果你使用 GNU 调试器选项进行编译...但即使这样,我也不认为该语言有这样做的结构。
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.
所以,这基本上就是我所做的。 虽然很hacky,但确实有效。 我创建了一个利用字符串化的可变参数宏。 不幸的是,它变得笨拙,需要一个 _dummy 参数来提供伪默认 ctor,因为你不能省略分隔命名参数和变量参数的逗号(我什至尝试使用 gnu cpp,但不成功 - 可能我还不够努力)。
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).