C++ gdb:显示一些数据的类型信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9568201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:59:40  来源:igfitidea点击:

gdb: show typeinfo of some data

c++gdbtypeidtypeinfo

提问by Albert

Basically, I want to get typeid(*this).name(), i.e. the real type of this.

基本上,我想得到typeid(*this).name(),即this.

I want to get this in GDB (without modifying the source code). I tried print typeid(*this)but it says that typeidis unknown (because I didn't included it there in the source file).

我想在 GDB 中得到这个(不修改源代码)。我试过了,print typeid(*this)但它说这typeid是未知的(因为我没有将它包含在源文件中)。

回答by Star Brilliant

Use ptypecommand, like this:

使用ptype命令,像这样:

(gdb) ptype 42
type = int

回答by t. fochtman

The 'ptype [ARG]' command will print the type.

'ptype [ARG]' 命令将打印类型。

回答by regnarg

This question may be related: vtable in polymorphic class of C++ using gdb:

这个问题可能与:vtable in polymorphic class of C++ using gdb

(gdb) help set print object
Set printing of object's derived type based on vtable info. 

It's not exactly typeid() but it should show the real object type when inspecting a polymorphic pointer (e.g. thisin a base class). Naturally works only for classes with a vtable (i.e. at least one virtual method) but so does typeid.

它不完全是 typeid() 但它应该在检查多态指针(例如this在基类中)时显示真实的对象类型。自然仅适用于具有 vtable(即至少一个虚拟方法)的类,但typeid.