c++ - typeid() 在 g++ 中返回额外的字符

标签 c++ g++ name-mangling typeid

class foo
{
public:
  void say_type_name()
  {
    std::cout << typeid(this).name() << std::endl;
  }
};

int main()
{
  foo f;;
  f.say_type_name();
}

上面的代码使用 g++ 在我的 ubuntu 机器上打印 P3foo。我不明白为什么它打印 P3foo 而不仅仅是 foo。如果我像

这样更改代码
    std::cout << typeid(*this).name() << std::endl;

它打印 3foo

有什么想法吗?

最佳答案

因为它是指向 foo 的指针。 foo 有 3 个字符。所以它变成了P3foo。另一个是 foo 类型,所以它变成 3foo。请注意,文本是依赖于实现的,在这种情况下,GCC 只是为您提供了内部的、错位的名称。

在程序 c++filt 中输入该损坏的名称以获得未损坏的名称:

$ c++filt -t P3foo
foo*

关于c++ - typeid() 在 g++ 中返回额外的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/789402/

相关文章:

c++ - 如何获取圆的半径值

c++ - 如果没有插入而只有 .find(),在多线程上使用 C++ STL 容器是否安全?

c++ - 使用带有仅 header 库的 Find*.cmake 文件

c++ - 如何生成具有继承和组合关系的C++类图?

c++ - 无法链接 GLFW3 : undefined references

c++ - 如何自动链接到共享库的依赖项

c++ - “to_string”是't a member of “std”吗?

python - 私有(private)名称修改有什么好处?

c++ - Visual Studio 中生成的匿名命名空间变量的损坏名称的数字部分是什么意思?

Python:动态类生成:覆盖成员