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

时间:2017-10-10 08:52:23

标签: c++

我正在一个遗留项目中工作,偶然发现一个用例进行修改。我的分析结论是一堂课。我能够追踪班级的一些方法,但随着我的进展,它在分析中变得有点乏味。

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

1 个答案:

答案 0 :(得分:0)

您可以使用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 in 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继承了什么。它并不好,但总比没有好。

相关问题