"Thinkinginc++" 75, multiple inheritance

Source: Internet
Author: User
Tags class definition

Nineth Chapter Multiple Inheritance 9.2 interface inheritance Intertfacees.cpp
/*** book: "thinkinginc++" * Function: Interface inheritance interfaces.cpp* time: October 28, 2014 20:06:31* Author: cutter_point*/#include <iostream># Include <sstream> #include <string> using namespace std;   Class Printable//abstract class {Public:virtual ~printable () {}//virtual function virtual void print (ostream&) const=0; pure virtual function}; Class Intable{public:virtual ~intable () {} virtual int toint () const=0;}; Class Stringable{public:virtual ~stringable () {} virtual String toString () const=0;};   Class Able:public Printable, publicintable, public stringable{int mydata;public:able (int x) {mydata=x;}   void print (ostream& os) const {os<<mydata;}   int ToInt () const {return myData;}       String toString () const {Ostringstream OS;       os<<mydata;    return Os.str (); }};   void testprintable (const printable& p) {p.print (cout); Cout<<endl;} void testintable (const intable& N) {cout<<n.toint () +1<<endl;} void teststringable (const stringable   &s) {Cout<<s.tostring () + "th" &LT;&LT;ENDL;}   int main () {Able A (7);   Testprintable (a);   Testintable (a);    Teststringable (a); return 0;}


9.5 Virtual base class

Mechanism of virtual inheritance.

in fact, the root cause of the two semantics of the above is in the special mode of inheritance, A This parent class is accompanied by B and the C produces two copies, which creates a contradiction in invoking the method in the copy, which is the print () it? So, everyone will think, if only one copy is good, there is no contradiction, virtual inheritance provides this mechanism, as the above example, just modify B and C pair A the inheritance, that is, add a keyword Virtual

Class B:  virtual public a{public    :        B () {cout << "b called" << Endl;}           Private:           };class C:  virtual public a{public    :        C () {cout << "C called" << Endl;}           Private:           };


this is tantamount to saying that in the absence of A the copy of the class constructs a, if already has, uses already the one, thus, copies only one copy, the ambiguity elimination.

Static_cast and dynamic_cast

Http://www.cnblogs.com/chio/archive/2007/07/18/822389.html

The dynamic_cast is primarily used for upstream and downstream conversions between class hierarchies, and can also be used for cross-conversion between classes.

The effect of dynamic_cast and static_cast is the same when upstream conversions are performed between class hierarchies, anddynamic_cast has the function of type checking in the downstream conversion , static_cast more secure.

The initialization order of child objects follows the following rules:

1) All Virtual base class sub-objects, initialized to bottom, left-to-right, according to where they appear in the class definition

2) then non-virtual base classes are initialized in the usual order

3) All member objects are initialized in the order declared.

4) Execution of the complete object's constructor function

As I said earlier, in order to initialize a child object of a base class, the constructor of the derived class calls the constructor of the base class. For virtual base classes, there is only one virtual base class sub-object in the object of the derived class. To ensure that the virtual base class sub-object is initialized only once, the virtual base class constructor must be called only once.

http://blog.csdn.net/maojudong/article/details/8169240

If class inheritance includes instances of more than one virtual base class, the base class is initialized only once.

1, if the class has a member class, the constructor of the member class is first called;

2, create the object of the derived class, the constructor function of the base class is called first (also takes precedence over the member class in the derived class);

3. base class constructors If there are multiple base classes, the order in which the constructors are called is a class that appears in the class-derived table.
order rather than the order in which they are in the Member initialization table;


4. Member class object constructors if there are more than one Member class object, the constructor is called in the order that the object is in the class
The order in which they appear in the member initialization table rather than the order in which they are declared;


5. Derived class constructors
As a general rule, a derived class constructor should not be able to assign values directly to a base class data member but to pass the value
To the appropriate base class constructor otherwise the implementation of the two classes becomes tightly coupled (tightly coupled) will be more difficult to
Modify or extend the implementation of the base class correctly. (The responsibility of the base class Designer is to provide a set of appropriate base class constructors)

VirtInit.cpp

On the initialization of virtual base class

/*** book: "thinkinginc++" * Function: Initialization of virtual base class * Time: October 28, 2014 20:07:25* Author: cutter_point*/#include <iostream> #include <string>using namespace Std;class m{public:m (const string& s) {cout<< "M" <<s<<endl;}    Each class has an embedded M-type member};class a{m;    Here is a class of combination public:a (const string& s): M ("in A") {cout<< "a" <<s<<endl;    Tracking initialization of class A} virtual ~a () {cout<< "destructor a" <<endl;}//This is a virtual base class};class b{M M;    Here is a class of combination public:b (const string& s): M ("in B") {cout<< "B" <<s<<endl;    Tracking initialization of Class A} virtual ~b () {cout<< "destructor B" <<endl;}//This is a virtual base class};class c{M M;    Here is a class of combination public:c (const string& s): M ("in C") {cout<< "C" <<s<<endl;    Tracking initialization of Class A} virtual ~c () {cout<< "destructor C" <<endl;}//This is a virtual base class};class d{M M; Here is a combination of a class public:d (const string& s): M ("in D") {cout<< "D" &LT;&LT;S&LT;&LT;endl; Tracking initialization of Class A} virtual ~d () {cout<< ' destructor D ' <<endl;}//This is a virtual base class};class f:virtual public B, virtual public C        , public D//virtual inheritance {M m;public:f (const string& s): B ("from F"), C (' from f '), D ("from F"), M ("in F") {    cout<< "F" <<s<<endl; }};//begins multiple inheritance class E:public A, virtual public B, virtual public C//virtual inheritance {M m;public:e (const string& s): A ("fr    Om e "), B (" from E "), C (' from E '), M (" in E ") {cout<<" E "<<s<<endl; }};//Final inheritance E,fclass g:public E, public f{M m;public://The Order of initialization here differs from the order of inheritance, looking at the result, the result is initializing G in the Order of inheritance (const string& s    ): B ("from G"), C (' from G '), F ("from G"), E (' from G '), M ("in G") {cout<< "G" <<s<<endl;    The order in which the}};int main () {//constructor is called is the order in which a class appears in the class-derived table, G g ("from main"); return 0;}


Important Conclusions:

Base class constructors If there is more than one base class, the constructor's call order is a class that appears in a class-derived table
order instead of their order in the member initialization table.

"Thinkinginc++" 75, multiple inheritance

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.