以程序方式检索c ++类名 [英] Retrieving a c++ class name programatically

查看:144
本文介绍了以程序方式检索c ++类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可能在C ++中以字符串形式检索类的名称,而不必将其硬编码为变量或getter。我知道没有一个信息实际上在运行时使用,因此它不可用,但有什么宏可以创建这个功能吗?感谢。



编辑:可能有帮助的是,我实际上正在尝试检索派生类的名称,并且我使用Visual C ++ 2008 Express Edition。

解决方案

您可以使用RTTI:

  #include< typeinfo> 
cout<< typeid(obj).name()< endl;然而,这是不鼓励的,因为格式不是标准化的,并且可能在不同的编译器之间不同(甚至是不同的编译器,甚至是不同的编译器)不同版本的相同编译器)。


I was wondering if it is possible in C++ to retrieve the name of a class in string form without having to hardcode it into a variable or a getter. I'm aware that none of that information is actually used at runtime, therefor it is unavailable, but are there any macros that can be made to create this functionality? Thanks.

Edit: May be helpful to note that I'm actually trying to retrieve the name of a derived class, and I'm using Visual C++ 2008 Express Edition.

解决方案

You can use RTTI:

#include <typeinfo>
cout << typeid(obj).name() << endl;

However, this is discouraged since the format isn't standardized and may differ between different compilers (or even different versions of the same compiler).

这篇关于以程序方式检索c ++类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆