"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 above is in the special pattern of such inheritance. a This parent class is accompanied by two copies of B and C , respectively. There is a contradiction in invoking the methods in the copy. Which copy of print () is it called ? So, all people will think, if only a copy is good, there is no contradiction. Virtual inheritance provides such a mechanism, according to the above example, only need to change B and C to The inheritance of a way, That adds 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:           };


that's the equivalent of saying. When there is no copy of Class A, construct one, assuming it already exists, and use the one that is already there, so that the copy is just one copy. The ambiguity was eliminated.

Static_cast and dynamic_cast

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

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



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

The initialization order of child objects follows such rules as the following:

1) All Virtual base class sub-objects, initialized to the next, 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) The complete object's constructor runs

As mentioned earlier, in order to initialize a child object of the base class. The constructor of the derived class to call the constructor of the base class.

For virtual base classes, because 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

Assume that class inheritance contains instances of multiple virtual base classes. The base class is initialized only once.

1. If there is a member class inside the class, the constructor of the member class is called first.

2. Create an object of the derived class. The constructor function of the base class is called first (also precedence over the member class in the derived class);

3, the base class constructor assumes that there are multiple base classes, the constructor is called in the order that a class appears in the class-derived table
order instead of their order in the member initialization table.


4, the Member class object constructor assumes that there are multiple member class objects, the constructor is called in the order that the object is in the class
The order in which they are declared, not the order in which they are present in the Member initialization table;


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
Correct changes or extensions to the base class implementation.

(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" <<s<< 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; }};//start 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; }};//finally inherits E,fclass G:public E, public f{M m;public://Here The order of initialization differs from the order of inheritance. Looking at the result, the result is initializing G (const string& s) in the Order of Inheritance: B ("from G"), C ("from G"), F ("from G"), E ("from G"), M ("in G") {Co    ut<< "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:

The base class constructor assumes that there are multiple base classes in which 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.