在 C++ 中,我可以使用 typeid
运算符来检索任何多态类的名称:
const char* name = typeid( CMyClass ).name();
返回的const char*
指向的字符串will be available to my program for as long as the corresponding class exists .
typeid(T).name()
的多次调用是否会为相同的 class T
返回相同的指针值,或者它们是否允许返回不同的指针?
最佳答案
不,它们不需要返回相同的指针。
我在野外系统也看到过没有。
每个 DLL 都有自己的包含 nmae 的类型对象拷贝,因此在共享库 A 和共享库 B 中调用 typeid(N).name 将导致不同的指针(尽管底层字符串相同)。
关于c++ - 多次调用 typeid(T).name() 会返回同一个指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778560/