证明vptr指针的存在
1 // 证明vptr指针的存在 2 3 #include <iostream> 4 5 using namespace std; 6 7 class parent { 8 public: 9 parent(int a) { 10 this->a = a; 11 } 12 13 virtual void pri(void) { 14 cout << "parent = "<< a << endl; 15 } 16 17 private: 18 int a; 19 }; 20 21 class child :public parent { 22 public: 23 child(int b) :parent(6) { 24 this->b = b; 25 } 26 27 virtual void pri(void) { 28 cout << "child = " << b << endl; 29 } 30 31 private: 32 int b; 33 }; 34 35 int main(void) 36 { 37 parent par(1); 38 child chi(2); 39 40 parent *p = NULL; 41 42 //p = ∥ 43 p = χ 44 45 p->pri(); 46 47 cout << sizeof(par) << endl; // 8个字节 = int + vpre 指针 (函数在代码段不占) 48 cout << sizeof(chi) << endl; //12个字节 = int + int + vptr指针(继承父类) 49 50 system("pause"); 51 52 return 0; 53 }
笔记