Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ָ נן oop C++ Addendum: Multiple and Virtual Inheritance in C++ uInitialization of Virtual Bases uFrozen Classes in C++ uConstruction Order with MI.

Similar presentations


Presentation on theme: "1 ָ נן oop C++ Addendum: Multiple and Virtual Inheritance in C++ uInitialization of Virtual Bases uFrozen Classes in C++ uConstruction Order with MI."— Presentation transcript:

1 1 ָ נן oop C++ Addendum: Multiple and Virtual Inheritance in C++ uInitialization of Virtual Bases uFrozen Classes in C++ uConstruction Order with MI

2 2 ָ נן oop Constructors and Virtual Base Class? Consider the following situation: class V { public: V(const char *s){ cout << s;} }; class D: public B1, public B2 { public: D(void): B1("DB1"), B2("DB2") {} } d; What will be printed? class B1: public virtual V { public: B1(const char *s): V("B1"){ cout << s; } }; class B2: public virtual V { public: B2(const char *s): V("B2"){ cout << s; } };

3 3 ָ נן oop uAnswer: nothing will be printed. The compiler will issue an error message. uWork around 1: lDefine a default constructor in V uWork around 2: lCall V ’s constructor directly from the constructor of D Virtual bases are initialized by the initializer of the most derived class, other initializers are ignored Virtual bases may be initialized in a constructor of a derived class even if they are not immediate base classes uComments: lAll virtual inheritances of the same base are unified. lAll non virtual inheritances of the same base are distinct. Construction of a Virtual Base Class

4 4 ָ נן oop Virtual Base Initilization class V { public: V(); V(int);... }; class A: public virtual V { public: A(); A(int i): V(i) { /*... */ }... }; class B : public virtual V { public: B(); B(int i) { /*... */ }... }; class C : public A, public B { public: C(int i): V(i) { /*... */ }... }; V v(1); // use V(int) A a(2); // use V(int) B b(3); // use V() C c(4); // use V(int) class V { public: V(); V(int);... }; class A: public virtual V { public: A(); A(int i): V(i) { /*... */ }... }; class B : public virtual V { public: B(); B(int i) { /*... */ }... }; class C : public A, public B { public: C(int i): V(i) { /*... */ }... }; V v(1); // use V(int) A a(2); // use V(int) B b(3); // use V() C c(4); // use V(int)

5 5 ָ נן oop Frozen Classes in C++ #ifndef ICE struct ice__ { ice__(void){}; // Constructor }; #define ICE private virtual ice__ #endif ICE #ifndef ICE struct ice__ { ice__(void){}; // Constructor }; #define ICE private virtual ice__ #endif ICE #include "ice.h" class Frozen: ICE { //... } var; class Violation: public Frozen { //... }; #include "ice.h" class Frozen: ICE { //... } var; class Violation: public Frozen { //... }; Error: ice__::ice__() is not accessible in function Violation::Violation() ice.h The trick may be easily worked around by deriving the Violation class from ice__

6 6 ָ נן oop Construction and Multiple Inheritance uElements to initialize: sub-objects, reference data members, const members, and all other data members. uWhere to initialize? best in constructor header (only other data members can be initialized in constructor body). uOrder of initializers in constructor header: unrelated to actual initialization sequence; this makes it possible to guarantee that the construction order is the exact opposite of destruction order. uConstruction Order: recursive algorithm lVirtual base classes: immediate and non-immediate Order is as they occur on a depth-first, left-to-right traversal of the DAG of base classes. If a virtual base class is derived from a non-virtual base, then this non-virtual base will be constructed before this virtual base is constructed. lOther base-classes (in order of definition). lData members (in order of definition). lConstructor body uOrder of destruction is the same in reverse.

7 7 ָ נן oop Initialization Order Algorithm uApply topological sort ranking inheritance DAG in a depth-first, left-right scan lVirtual and non-virtual inheritance are treated alike. uConstruct all virtual base classes (immediate and non-immediate) lUse ranking order. lDo not construct twice. lApply recursively to construct their non-virtual bases. uConstruct non-virtual immediate base classes: lUse ranking order (same as definition order). lApply recursively to construct their non-virtual bases. uConstruction order in example: U1 U2 Y X V2 V1 V3 V4 B1 B2 D uDestruction order in example: D B2 B1 V4 V3 V1 V2 X Y U2 U1


Download ppt "1 ָ נן oop C++ Addendum: Multiple and Virtual Inheritance in C++ uInitialization of Virtual Bases uFrozen Classes in C++ uConstruction Order with MI."

Similar presentations


Ads by Google