I saw the stuffy chicken, writing something, thinking about thinking

xiaoxiao2021-03-06  55

first time:

/ * * Conclusion: * The data of the class object can be cut (SLICE), but does not affect the virtual table (Virtual Table). * Saves the pointer of the virtual table in the data area of ​​the class object, and the responsibilities of the class object are only. * / # Include

Using namespace std;

Class a {public: void print () {cout << "this is a." <

}

}

Class B: Public a {public: void print () {cout << "this is b."

}

}

Class va {public: Virtual void print () {cout << "this is a."

}

}

Class VB: PUBLIC VA {public: void print () {cout << "this is b."

}

}

INT main (void) {a _a; b_b; a * _pa = & _A; // Pointer to the _a object.

/ / Pointer to a pointer to the _b object, but the data size accessed is SIZEOF (a) a * _PB = & _b;

VA _VA; VB _VB; VA * _VPA = & _VA;

// Pointer to a pointer to the _vb object, but the data size accessed is SIZEOF (VA) VA * _VPB = & _vb;

_PA-> Print (); _PB-> print ();

_VPA-> Print (); _vpb-> print ();

Return 0;}

The second article:

#include

Using namespace std;

Class a {public: Virtual void fun () {cout << "A :: fun" <

Virtual void fun2 () {cout << "A :: fun2" <

}

Class B: Public a {public: void fun () {cout << "b :: fun" <

Void fun2 () {cout << "b :: fun2" <

}

INT main (void) {void (); // Defines function pointer A * p = new B within a class A;

// Returns should be the index value of Slot in a VTBL, actual and A, B have no relationship fun = & a :: fun; (p -> * fun) (); // Compiler internal calculation: P -> (VPTR index) (); / * * Note: At this point P pointing to a class A object, but VTBL is the function pointer of class B.

* Or the above old saying, class object (data) only maintains VPTR, as for function addresses, run

* Calculated according to Index. * / Fun = & a :: fun2; (p -> * fun) (); delete p;

Return 0;}

Third article:

DELETE P; Compiler Maintains recycling SIZEOF (B) size in internal state, not SIZEOF (A).

If there is something wrong, please high-handed ax.

转载请注明原文地址:https://www.9cbs.com/read-115417.html

New Post(0)