C++ 如何获取类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4466088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:28:44  来源:igfitidea点击:

How to get class name?

c++classname

提问by sivabudh

If I defined a class:

如果我定义了一个类:

class Blah {};

How can I:

我怎样才能:

std::string const className = /* What do I need to do here? */;
assert( className == "Blah" );

I dont think typeid().name() is a good idea since it's compiler implementationspecific. Is there anything provided by the C++ standard or Boost?

我不认为 typeid().name() 是一个好主意,因为它是特定于编译器实现的。C++ 标准或 Boost 是否提供了任何内容?

Note: If the class were inherited from Qt's QObject, I could easily use QMetaObject::className()to get the class name.

注意:如果类是从 Qt 的 QObject 继承的,我可以很容易地使用它QMetaObject::className()来获取类名。

采纳答案by Edward Strange

Like this:

像这样:

class Blah { static std::string const className() { return "Blah"; }};

std::string const name = Blah::className();
assert(name == "Blah");

Or this:

或这个:

class Blah {};

template < typename T > struct name;
template < > struct name<Blah> { static std::string value() { return "Blah"; }};

std::string const classname = name<Blah>::value();
assert(classname == "Blah");

Fancier:

鸽友:

#define DECLARE_NAMED_CLASS( Name ) \
struct Name;\
template < > struct name<Name> { static std::string value() { return #Name; }};\
struct Name

DECLARE_NAMED_CLASS(Blah) {};
std::string const className = name<Blah>::value();
...

Or this:

或这个:

class Blah : QObject { Q_OBJECT };

Or this:... Or this: ...

或者这个:... 或者这个:...

回答by Nim

Testing a class by looking at it's name sounds awfully like a Java style approach to me, and in C++, you should be wary of trying to apply the same patterns! A better way would be to use something like boost::type_traits, and may be is_same, with the real class name.

通过查看类的名称来测试类对我来说听起来非常像 Java 风格的方法,在 C++ 中,您应该警惕尝试应用相同的模式!更好的方法是使用类似boost::type_traits,并且可能是is_same,具有真实类名的东西。

回答by J?rgen Fogh

I think a dynamic_castmay be what you are looking for. It does not give you the name of the class, but it fails in the way you would like your assertion to fail, except that subclasses of Blahwill not be caught.

我认为 adynamic_cast可能是你正在寻找的。它不会为您提供类的名称,但它以您希望断言失败的方式失败,除了Blah不会捕获 的子类。

回答by Gene Bushuyev

I dont think typeid().name() is a good idea since it's compiler implementation specific.

我不认为 typeid().name() 是一个好主意,因为它是特定于编译器实现的。

Yes, standard doesn't require from implementation to use any specific naming, so it may change even for the same compiler.

是的,标准不需要从实现中使用任何特定的命名,因此即使对于相同的编译器,它也可能会发生变化。

Is there anything provided by the C++ standard or Boost?

C++ 标准或 Boost 是否提供了任何内容?

There are no standard facilities that would return class name in some canonic form.

没有标准工具会以某种规范形式返回类名。

回答by MasterMito

The QObject->metaObject() method is valid for Qt except the QGraphicsItem based classes that not inherit from QObject...

QObject->metaObject() 方法对 Qt 有效,除了不是从 QObject 继承的基于 QGraphicsItem 的类...

回答by Matteo Italia

I don't think there's any non-compiler specific solution to such problem that does not involve plenty of macros in the class declaration (actually if I understood correctly the QT documentation the string you get with objectNameis actually assigned "by hand", I think with code created by moc).

我不认为有任何非编译器特定的解决方案来解决此类问题,在类声明中不涉及大量宏(实际上,如果我正确理解 QT 文档,您获得的字符串objectName实际上是“手动”分配的,我认为使用由moc)创建的代码。

On the other hand, in general to check if the class of an object is one you don't want you shouldn't do a string comparison, but instead make a typeid comparison.

另一方面,通常要检查对象的类是否是您不想要的类,您不应该进行字符串比较,而是进行 typeid 比较。

assert(typeid(YourObject)==typeid(Blah));

But probably you should explain better what you're trying to achieve.

但也许你应该更好地解释你想要实现的目标。