标签:c++ 用于大型程序的工具 多重继承 虚继承 特殊初始化语义
七、特殊的初始化语义
从具有虚基类的类继承的类对初始化进行特殊处理:在虚基类中,由最低层派生类的构造函数初始化虚基类。在ZooAnimal示例中,使用常规规则将导致Bear 类和 Raccoon类都试图初始化Panda对象的ZooAnimal类部分。
虽然由最低层派生类初始化虚基类,但是任何直接或间接继承虚基类的类一般也必须为该基类提供自己的初始化式。只要可以创建虚基类派生类类型的独立对象,该类就必须初始化自己的虚基类,这些初始化只在创建中间类型的对象时使用。
Bear::Bear(std::string name, bool onExhibit): ZooAnimal(name, onExhibit, "Bear") { } Raccoon::Raccoon(std::string name, bool onExhibit): ZooAnimal(name, onExhibit, "Raccoon") { }
虽然ZooAnimal不是Panda的直接基类,但是Panda构造函数也初始化ZooAnimal基类:Panda::Panda(std::stringname, bool onExhibit):
ZooAnimal(name, onExhibit, "Panda"), Bear(name, onExhibit), Raccoon(name, onExhibit), Endangered(Endangered::critical), sleeping_flag(false) { }
1、怎样构造虚继承的对象
Bear winnie("pooh"); Raccoon meeko("meeko"); Panda yolo("yolo");
class Character { //... }; class BookCharacter : public Character { //... }; class ToyAnimal { //... }; class TeddyBear : public BookCharacter,public Bear, virtual public ToyAnimal { //... };
按声明次序检查直接基类,确定是否存在虚基类。按从根类开始向下到最低层派生类的次序检查每个子树。
TeddyBear的虚基类的构造次序是先ZooAnimal再ToyAnimal。一旦构造了虚基类,就按声明次序调用非虚基类的构造函数:首先是BookCharacter,它导致调用Character构造函数,然后是Bear。因此,为了创建TeddyBear对象,按下面次序调用构造函数:
ZooAnimal(); ToyAnimal(); Character(); BookCharacter(); Bear(); TeddyBear();
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构造次序 Class(); Base(); Derived1(); Derived2(); MI(); Class(); Final();
//a析构次序 ~Finale(); ~Class(); ~MI(); ~Derived2(); ~Derived1(); ~Base(); ~Class();
//b:一个Final对象有一个Base对象,两个Class对象。
//c Base *pb; Class *pc; MI *pmi; Derived2 *pd2; pb = new Class; //Error pmi = pb; //Error pc = new Final; //Error pd2 = pmi; //OK
//习题17.36 class 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) {} //... };
C++ Primer 学习笔记_97_用于大型程序的工具 --多重继承与虚继承[续2],布布扣,bubuko.com
C++ Primer 学习笔记_97_用于大型程序的工具 --多重继承与虚继承[续2]
标签:c++ 用于大型程序的工具 多重继承 虚继承 特殊初始化语义
原文地址:http://blog.csdn.net/hanqing280441589/article/details/27948539