typeid() 在 g++ 中返回额外的字符 [英] typeid() returns extra characters in g++

查看:20
本文介绍了typeid() 在 g++ 中返回额外的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.如果我像

Above code prints P3foo on my ubuntu machine with g++. I am not getting why it is printing P3foo instead of just foo. If I change the code like

    std::cout << typeid(*this).name() << std::endl;

它打印 3foo.

有什么想法吗?

推荐答案

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

Because it is a pointer to foo. And foo has 3 characters. So it becomes P3foo. The other one has type foo, so it becomes 3foo. Note that the text is implementation dependent, and in this case GCC just gives you the internal, mangled name.

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

Enter that mangled name into the program c++filt to get the unmangled name:

$ c++filt -t P3foo
foo*

这篇关于typeid() 在 g++ 中返回额外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆