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++ 中返回额外的字符
基础教程推荐
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
