列出派生类中的所有方法/变量

List all methods / variables in a derived class

本文关键字:有方法 变量 派生      更新时间:2023-10-16

我正在一个遗留项目中工作,偶然发现了一个修改用例。我的分析总结到一堂课。我能够跟踪该类的一些方法,但随着我的继续,它在分析中变得有点乏味。

是否有一种机制/方法可以让我知道给定类的所有方法(用户定义/继承等)及其成员变量,尤其是对于 Linux 平台?

您可以使用 gdb,但这意味着您必须导航到代码的该部分。如果我有一个小程序:

struct AAA {
    int iii;
    int aonly;
    void foo () {
    }
};
struct BBB: public AAA {
    int iii;
    int bonly;
    void fooB () {
    }
};
int main (int argc, const char* argv[]) {
    BBB b;
    b.iii = 1;
    return 0;
}

我可以使用调试符号(g++ 中的 -g)进行编译,设置断点并打印对象:

eric@mouni2:/tmp/ttt$ g++ -g a.cpp
eric@mouni2:/tmp/ttt$ gdb a.out
*** output flushed ***
(gdb) b 18
Breakpoint 1 at 0x4004e8: file a.cpp, line 18.
(gdb) R
Starting program: /tmp/ttt/a.out 
Breakpoint 1, main (argc=1, argv=0x7fffffffe278) at a.cpp:18
18      return 0;
(gdb) ptype b
type = struct BBB : public AAA {
    int iii;
    int bonly;
  public:
    void fooB(void);
}
(gdb) p b
$1 = {<AAA> = {iii = -7568, aonly = 32767}, iii = 1, bonly = 0}
(gdb) p b.iii
$2 = 1
(gdb) p b.AAA::iii
$3 = -7568

在那里你可以看到BBB从AAA继承了什么。这不是很好,但总比没有好。