C ++ Primer study note _ 97 _ tools for large programs

Source: Internet
Author: User

Tools for large programs-Multi-inheritance and virtual inheritance [continued 2]

VII. Special initialization Semantics

Class inherited from a class with a virtual base class performs special processing on Initialization: In the virtual base class,Initialize the virtual base class by the constructor of the lowest-layer derived class. In the ZooAnimal example, using regular rules will cause both the Bear class and the Raccoon class to attempt to initialize the ZooAnimal class part of the Panda object.

Although the virtual base class is initialized by the lowest-layer derived classAny class that directly or indirectly inherits the virtual base classYou must also provide your own initialization for the base class.As longYesCreateTheIndependent object, This class is requiredInitialize your own virtual base classThese initialization methods are only used when an intermediate type object is created.

Bear::Bear(std::string name, bool onExhibit):    ZooAnimal(name, onExhibit, "Bear") { }Raccoon::Raccoon(std::string name, bool onExhibit):    ZooAnimal(name, onExhibit, "Raccoon") { }

Although ZooAnimal is not a direct base class of Panda, the Panda constructor also initializes the ZooAnimal base class: Panda (std: stringname, bool onExhibit ):

    ZooAnimal(name, onExhibit, "Panda"),    Bear(name, onExhibit),    Raccoon(name, onExhibit),    Endangered(Endangered::critical),    sleeping_flag(false) { }

1. How to construct virtual inherited objects

    Bear winnie("pooh");    Raccoon meeko("meeko");    Panda yolo("yolo");

When creating a Panda object:
1. First, use the initialization type specified in the constructor initialization list to construct the ZooAnimal part.
2. Construct the Bear part. Ignore Bear's initialization type used for ZooAnimal constructor initialization list.
3. Then, construct the Raccoon part and ignore the ZooAnimal initialization type again.
4. Finally, construct the Panda part.
If the Panda constructor does not explicitly initialize the ZooAnimal base class, use the ZooAnimal default constructor. If ZooAnimal does not have the default constructor, the Code fails.


2. constructor and destructor order
No matter where the virtual base class appears in the inheritance level, the virtual base class is always constructed before the non-virtual function is constructed!
For example, the following irregular TeddyBear contains two virtual base classes: ToyAnimal base class and ZooAnimal indirect base class derived from Bear:

class Character{    //...};class BookCharacter : public Character{    //...};class ToyAnimal{    //...};class TeddyBear : public BookCharacter,public Bear,    virtual public ToyAnimal{    //...};

Check the direct base class in declared order,Check whether a virtual base class exists.. Check each subtree in the order of derived classes from the root class to the lowest layer.

The Constructing order of the TeddyBear virtual base class is ZooAnimal and then ToyAnimal. Once a virtual base class is constructedCall constructors of non-virtual base classes in declared order: First, BookCharacter, which calls the Character constructor and then Bear. Therefore, to create a TeddyBear object, the constructor is called in the following order:

 ZooAnimal();  ToyAnimal();  Character();  BookCharacter();  Bear();  TeddyBear(); 

Here, TeddyBear, a derived class from the lowest layer, specifies the initialization formula used for ZooAnimal and ToyAnimal.
In the synthesis copy constructor, the same construction order is used. In the synthesis assignment operator, the base class is assigned a value in this order. Ensure that the calling order of the base class destructor is the opposite to that of the constructor.


P627 exercise 17.35 hierarchy

class Class{    //...};class Base : public Class{    //...};class Derived1 : virtual public Base{    //...};class Derived2 : virtual public Base{    //...};class MI : public Derived1,public Derived2{    //...};class Final : public MI,public Class{    //...};

// A construct the sequence Class (); Base (); Derived1 (); Derived2 (); MI (); Class (); Final ();

// A structure order ~ Finale ();~ Class ();~ MI ();~ Derived2 ();~ Derived1 ();~ Base ();~ Class ();

// B: a Final object has a Base object and two Class objects.

//c    Base *pb;    Class *pc;    MI *pmi;    Derived2 *pd2;    pb = new Class;   //Error    pmi = pb;         //Error    pc = new Final;   //Error    pd2 = pmi;  //OK

// Exercise 17.36 class {//...}; class Base: public Class {public: Base (); Base (std: string); Base (const Base &); protected: std: string name; //...}; class Derived1: virtual public Base {public: Derived1 (): Base () {} Derived1 (std: string & s): Base (s) {} Derived1 (const Derived1 & d): Base (d ){}//...}; class Derived2: virtual public Base {public: Derived2 (): Base () {} Derived2 (std: string & s): Base (s) {} Derived2 (const Derived2 & d): Base (d ){}//...}; class MI: public Derived1, public Derived2 {public: MI (): Base () {} MI (std: string & s): Base (s), Derived1 (s ), derived2 (s) {} MI (const MI & m): Base (m), Derived1 (m), Derived2 (m ){}//...}; class Final: public MI, public Class {public: Final (): Base () {} Final (std: string & s): Base (s), MI (s) {} Final (const Final & f): Base (f), MI (f ){}//...};

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.