MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

base_code/virtual_base.cpp:44–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42
43
44int main()
45{
46 /** @brief 基类指针指向派生类对象,永远指向的都是派生类基类部分数据的起始地址
47 * 1. windows 下 VS 编译器会有问题,delete p 从 vfptr 开始,但是 new 的其实地址是 vbptr
48 * 2. Linux 下 g++ 编译器没有问题,delete p 会从 vbptr 开始释放内存
49 *
50 * B 的内存布局
51 * +---
52 * | {vbptr} virtual base ptr
53 * | mb
54 * +---
55 * +--- (virutal base A)
56 * | {vfptr} virtual function ptr
57 * | ma
58 * +---
59 *
60 * B::$vbtable@:
61 * | 0 (vbptr 起始偏移量,因为在最上面所以偏移量为 0)
62 * | 8
63 *
64 * B::$vftable@:
65 * | -16 (vptr 起始偏移量,vptr 上面有`vbptr`--8字节和`mb`--4字节,对齐为 8 字节,所以偏移量为 -16)
66 * | &B::show
67 * */
68 A *p = new B();
69 cout << "A *p " << p << endl;
70 p->show();
71 delete p;
72
73 return 0;
74}

Callers

nothing calls this directly

Calls 1

showMethod · 0.45

Tested by

no test coverage detected