我一直在谷歌上搜索并检查 gdb 手册,但似乎无法找到我正在尝试做的事情的答案。
有没有办法让 gdb 打印出给定类类型的所有方法的列表? print 命令似乎只显示数据成员和字段,没有显示任何方法。
此外,更进一步,有没有办法打印给定基 * 指针的所有正确虚拟方法?比如说:
struct A
{
virtual void foo() {}
};
struct B : public A
{
void foo() {}
};
int main()
{
A *b = new B;
}
如何让 gdb 打印变量 *b 并让它显示正确的虚拟方法?
谢谢
最佳答案
你可以使用ptype
。
假设我将这些行添加到您的示例中:
A alpha;
B beta;
现在在 gdb 中,我可以要求对类类型(或类类型的实例)进行描述:
(gdb) ptype alpha
type = class A {
public:
virtual void foo();
}
(gdb) ptype A
type = class A {
public:
virtual void foo();
}
(gdb) ptype beta
type = class B : public A {
public:
virtual void foo();
}
(gdb) ptype B
type = class B : public A {
public:
virtual void foo();
}
如果我用指针尝试,我会得到声明的类型:
(gdb) ptype b
type = class A {
public:
virtual void foo();
} *
如果我想要真正的类型,我必须设置`print object'变量:
(gdb) set print object on
(gdb) ptype b
type = /* real type = B * */
class A {
public:
virtual void foo();
} *
然后再调用ptype
,看看B
有什么(一步都不知道怎么弄)。
关于c++ - 如何在 gdb 中列出类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3956024/