【发布时间】:2013-09-25 13:04:06
【问题描述】:
我在 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
我得到与之前相同的输出。还有其他解决方案吗?
【问题讨论】:
-
Name mangling for types 并没有那么复杂,在那种情况下肯定不会......我不知道你的问题的答案是什么,但一种解决方法是自己阅读类型。
P指向6Circle圆对象的指针(6 是名称的长度)...P指向8triangle三角形的指针(8 个字符)。 -
嗯,这很简单。谢谢,但只是想知道是否有更清洁的方法来获得相同的效果
标签: c++ typeid typeinfo demangler c++filt