I. the compiler's polymorphism in C ++ is achieved by inserting a vptr pointer in the class and generating a virtual table so that the vptr points to the virtual table, so what kind of class is necessary to set the vptr pointer? As shown in:
The following six cases are discussed:
(1) base classes with virtual functions
Class A {<br/> Public: <br/> virtual void Foo () {}< br/> protected: <br/> int I; <br/> };
(2) base classes without virtual functions
Class A {<br/> Public: <br/> void Foo () {}< br/> protected: <br/> int I; <br/> };
(3) virtual inheritance. The base class has vptr.
Class base {<br/> Public: <br/> virtual ~ Base () {}< br/> virtual void Foo () {}< br/> virtual void bar () {}< br/> protected: <br/> int I; <br/>}; <br/> class derived: public virtual base {<br/> Public: <br/> ~ Derived () {}< br/> void bar () {}< br/> protected: <br/> Int J; <br/>}; <br/>
(4) non-virtual inheritance, no vptr for the base class, and no virtual function for the derived class
Class base {<br/> Public: <br/> ~ Base () {}< br/> void Foo () {}< br/> void bar () {}< br/> protected: <br/> int I; <br/>}; <br/> class derived: public base {<br/> Public: <br/> ~ Derived () {}< br/> void bar () {}< br/> protected: <br/> Int J; <br/>}; <br/>
(5) Non-virtual inheritance, no vptr for the base class, and a virtual function for the derived class
Class base {<br/> Public: <br/> ~ Base () {}< br/> void Foo () {}< br/> void bar () {}< br/> protected: <br/> int I; <br/>}; <br/> class derived: public base {<br/> Public: <br/> ~ Derived () {}< br/> virtual void bar () {}< br/> protected: <br/> Int J; <br/>}; <br/>
(6) Non-virtual inheritance. The base class has vptr.
Class base1 {<br/> Public: <br/> virtual ~ Base1 () {}< br/> virtual void Foo () {}< br/> virtual base1 * clone () const {}< br/> protected: <br/> int I; <br/>}; <br/> class base2 {<br/> Public: <br/> virtual ~ Base2 () {}< br/> virtual void bar () {}< br/> virtual base2 * clone () const {}< br/> protected: <br/> Int J; <br/>}; <br/> class derived: Public base1, public base2 {<br/> Public: <br/> virtual ~ Derived () {}< br/> virtual derived * clone () const {}< br/> protected: <br/> int K; <br/> }; <br/>