C++ c++;filt不要求使用typeid名称
我在GCC C++编译器上运行一个代码,输出Type信息::C++ c++;filt不要求使用typeid名称,c++,typeid,typeinfo,demangler,c++filt,C++,Typeid,Typeinfo,Demangler,C++filt,我在GCC C++编译器上运行一个代码,输出Type信息:: #include <iostream> #include <typeinfo> using namespace std; class shape { protected: int color; public: virtual void draw() = 0; }; class Circle: public shape { protected: int color
#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';
}
一经要求,
./shape | c++filt
我得到了与前面相同的输出。还有其他解决方案吗?对于类型,您需要使用
c++filt-t
,因此以下方法应该有效:
./shape | c++filt -t
对于-t
,系统会显示以下内容:
尝试demangle类型和函数名。这在默认情况下是禁用的,因为损坏的类型通常只在编译器内部使用,并且可能会与未损坏的名称混淆。例如,一个名为“a”的函数被视为一个损坏的类型名,它将被要求为“signed char”
您使用的是哪个版本的GCC(及其对应的libstdc++)
在GCC4.8中,我有
static inline std::string
demangled_type_info_name(const std::type_info&ti)
{
int status = 0;
return abi::__cxa_demangle(ti.name(),0,0,&status);
}
然后我可以使用
std::cout << demangled_type_info_name(typeid(*ptr)) << std::endl;
std::cout for type并没有那么复杂,在这种情况下肯定不会。。。我不知道你的问题的答案是什么,但一个解决办法是自己阅读类型
P
指向6Circle
Circle对象的指针(6是名称的长度)P
指向8triangle
triangle(8个字符)的指针。嗯,这很简单。谢谢,但我只是想知道是否有一种更干净的方法可以达到同样的效果。你应该升级到GCC 4.8.1,因为typeid
在那里实现得更好。祝贺你,你刚刚诱导了数百人将内存泄漏添加到他们的应用程序中。创建返回字符串后,必须释放由abi::\uuucxa\udmangle
返回的缓冲区。
std::cout << demangled_type_info_name(typeid(*ptr)) << std::endl;