[英]C++ equivalent of Java's this.getClass().getSimpleName();
In Java, if I want to use the object class' name I can write: 在Java中,如果要使用对象类的名称,可以编写:
String myString = this.getClass().getSimpleName();
What's the C++ equivalent of this? C ++的等效功能是什么? That is, how can I get a string with the name of the class of
*this
? 也就是说,如何获得带有
*this
类名称的字符串?
There is no equivalent in standard C++. 标准C ++中没有等效功能。 The closest you'll get is
typeid(*this).name()
, but there's no guarantee about what the "name" will look like - the result is implementation-defined. 您将得到的最接近的类型是
typeid(*this).name()
,但是不能保证“名称”的外观是什么-结果是实现定义的。 It will basically be whatever your compiler decided to put there, which may or may not correspond to the class's name in the source code. 基本上,它将是您的编译器决定放置在此处的内容,无论该内容可能与源代码中的类名称相对应。 See cppreference.com for more information.
有关更多信息,请参见cppreference.com 。
If you want to know the name of a class whose code you can change, you could write a function that returns a readable name. 如果您想知道可以更改其代码的类的名称,则可以编写一个返回可读名称的函数。 But otherwise, you're basically stuck with whatever the code above returns.
但是否则,您基本上会陷入上面代码返回的任何问题。
A在哪里
typeid(A).name()
We can Create our own Function For that, 为此,我们可以创建自己的功能,
class ClassName
{
static std::string const className()
{
return "ClassName";
}
};
std::string const name = ClassName::className();
The QObject->metaObject()
method is valid for Qt
except the QGraphicsItem
based classes that not inherit from QObject
. 除了不继承自
QObject
基于QGraphicsItem
的类之外, QObject->metaObject()
方法对Qt
有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.