Deep understanding of C ++ Polymorphism

Source: Internet
Author: User

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/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.