- 论坛徽章:
- 14
|
本帖最后由 lxyscls 于 2015-08-19 09:29 编辑
回复 1# demilich - #include <iostream>
- using namespace std;
- class A
- {
- public:
- virtual void f()
- {
- cout << "A" << endl;
- }
- };
- class B: public A
- {
- public:
- virtual void f()
- {
- cout << "B" << endl;
- }
-
- void e()
- {
- cout << x << endl;
- }
- private:
- int x = 10;
- };
- int main(void)
- {
- A *pa = new A;
- B *pb = (B*)pa;
- pb->f();
- pb->e();
- B *pc = new B();
- pc->e();
- return 0;
- }
复制代码 输出是:
A
0
10
GDB:- (gdb) b B::e
- Breakpoint 1 at 0x400b1a: file test.cpp, line 24.
- (gdb) r
- Starting program: /root/cpp/test
- A
- Breakpoint 1, B::e (this=0x603010) at test.cpp:24
- 24 cout << x << endl;
- Missing separate debuginfos, use: debuginfo-install glibc-2.18-19.fc20.x86_64 libgcc-4.8.3-7.fc20.x86_64 libstdc++-4.8.3-7.fc20.x86_64
- (gdb) p &x
- $1 = (int *) 0x603018
- (gdb) x/10 0x603018
- 0x603018: [color=Red]0[/color] 0 0 0
- 0x603028: 135137 0 0 0
- 0x603038: 0 0
- (gdb) c
- Continuing.
- 0
- Breakpoint 1, B::e (this=0x603030) at test.cpp:24
- 24 cout << x << endl;
- (gdb) p &x
- $2 = (int *) 0x603038
- (gdb) x/10 0x603038
- 0x603038: [color=Red]10[/color] 0 0 0
- 0x603048: 135105 0 0 0
- 0x603058: 0 0
- (gdb)
复制代码 |
|