我在 GCC C++ 编译器上运行代码,以输出 type_info::name:
#include <iostream>
#include <typeinfo>
using namespace std;
class shape {
protected:
int color;
public:
virtual void draw() = 0;
};
class Circle: public shape {
protected:
int color;
public:
Circle(int a = 0): color(a) {};
void draw();
};
void Circle::draw() {
cout<<"color: "<<color<<'\n';
}
class triangle: public shape {
protected:
int color;
public:
triangle(int a = 0): color(a) {};
void draw();
};
void triangle::draw() {
cout<<"color: "<<color<<'\n';
}
int main() {
Circle* a;
triangle* b;
cout<<typeid(a).name()<<'\n';
cout<<typeid(b).name()<<'\n';
}
但我得到以下结果:
P6Circle
P8triangle
关于拆线,
./shape | c++filt
我得到了与之前相同的输出。还有其他解决方案吗?
最佳答案
您需要对类型使用 c++filt -t
,因此以下内容应该有效:
./shape | c++filt -t
man page for c++filt对 -t
说以下内容:
Attempt to demangle types as well as function names. This is disabled by default since mangled types are normally only used internally in the compiler, and they can be confused with non-mangled names. For example, a function called "a" treated as a mangled type name would be demangled to "signed char".
关于c++filt 不 demangle typeid 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19005744/