【问题标题】:List all methods / variables in a derived class列出派生类中的所有方法/变量
【发布时间】:2017-10-10 08:52:23
【问题描述】:

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

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

【问题讨论】:

  • 每个 IDE 都应该为您完成这项工作。带有 ctags 的 vim 也能够为您提供定义列表。最后一个可能没有继承的......
  • 通读定义,父层次结构中每个类的定义是最简单的方法,但如果成员函数是内联定义的,可能会涉及大量代码。

标签: c++


【解决方案1】:

您可以使用 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 继承的内容。这不是很好,但总比没有好。

【讨论】:

  • 非常感谢您的指点。我可以调试对象创建本身 - 正在调用哪些类构造函数并继续
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多