c++filt 不 demangle typeid 名称

标签 c++ typeid typeinfo demangler c++filt

我在 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/

相关文章:

c++ - 如何在默认绘制结果之上的子类控件绘制方法中绘制某些内容?

C++ 确定变量的类型并在 sizeof() 中使用它

c++ - 如何使用 typeinfo.name C++ 声明变量

c++ - 在 C++ 中调用作为参数传递的同一模板函数的两个版本

c++ - g++ ld 共享库错误代码:: block

c++ - 派生类和类型检查

delphi - 具有指定值的枚举类型没有TypeInfo,为什么?

存储 type_info 对象的 C++ 方法不起作用

c++ - Qt 在状态栏中显示不断更新的百分比

c++ - 验证变量类型