Effective C + + clause 40

Source: Internet
Author: User

The articles in this section describe the use of multiple inheritance
Multiple inheritance is rarely used in general, because multiple inheritance is prone to procedural errors. To go down two typical call errors:
The first error is the following code:

 #include <iostream>  using  namespace Std;class b{public : Span class= "Hljs-keyword" >virtual  int  m  ( {}};class C {public : virtual  int  m  () {};}; Class D: public  b,private  c{public :};    int  main () {D D; D.M ();    //compilation does not pass because M () call is ambiguous  return  0 ;}  

As in the code, because there is a M () function in class B, there is also a M () function in Class C, and D inherits the M () function of both B and D, so it is not possible to determine which one to invoke at the time of invocation. Although D inherits C is private, D cannot call the M () function of C, but function matching is handled before function callable, and the M () function of two classes is the best match, so the system cannot choose which function to call.

The second type of error code:
As follows:

class A{public:  int m;};class B:public A{public:};class C :public A{public:};class D :public B,private C{public:};

As the code above, B and C inherit from A,d and inherit from B and C, so for the data in a, there will be two copies in D. For example, int m, to prevent this from happening, you can add the virtual keyword to avoid the inheritance relationship.
The following code:

 #include <iostream>  using  namespace Std;class a{public :}; Class B:virtual  public  a{ public : virtual   int  m  () {}};class C: virtual  public  a{public : virtual  int  m  () {};}; Class D: public  b,private  c{public :};  

And the use of the virtual keyword to the program will have side effects, side effects are
1. Objects with virtual inheritance are larger than objects inherited by non-virtual
2. Accessing the virtual base classes member variable is slower than accessing the Non-virtual base classes member variable.

The use of multiple inheritance is sometimes appropriate, which can increase the reuse rate of the program and take a look at the examples in the book:

Class iperson{ Public:Virtual~IPerson();VirtualSTD::stringName ()Const=0;VirtualSTD::stringBirthDate ()Const=0;    };    Class databaseid{...}; Class personinfo{ Public:Explicit PersonInfo(DatabaseID pid);Virtual~personinfo ();Virtual Const Char* Thename ()Const;Virtual Const Char* Thebirthdaydate ()Const; ......Private:Virtual Const Char*Valuedelimopen()Const;Virtual Const Char* Valuedelimclose ()Const;    ...... }; Class CPerson: PublicIPerson,PrivatePersonInfo { Public:Explicit CPerson(DatabaseID pid):PersonInfo(PID) {}VirtualSTD::stringName ()Const{returnPersoninfo::thename (); }VirtualSTD::stringBirthDate () {returnPersoninfo::thebirthdate (); }Private:Const Char*Valuedelimopen()Const{return "";}Const Char* Valuedelimclose ()Const{return "";} };

Effective C + + clause 40

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.