Ste*_*cob 7 c++ typeid typeinfo c++filt demangler
我在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';
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下结果:
P6Circle
P8triangle
Run Code Online (Sandbox Code Playgroud)
并且在消解时,
./shape | c++filt
Run Code Online (Sandbox Code Playgroud)
我得到了与之前相同的输出.还有其他方法吗?
Sha*_*our 12
你需要使用c++filt -t
类型,所以以下应该工作:
./shape | c++filt -t
Run Code Online (Sandbox Code Playgroud)
尝试解码类型和函数名称.默认情况下禁用此选项,因为损坏的类型通常仅在编译器内部使用,并且可能与非损坏的名称混淆.例如,一个被称为"a"的函数被视为一个受损的类型名称,将被解构为"signed char".