typeid() returns extra characters in g++(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++ 中返回额外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:typeid() 在 g++ 中返回额外的字符
基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
