Construction (construct) and destructor (destruct) Order of virtual inheritance (virtual inheritance)
In multiple inheritance, the constructor order is virtual inheritance in order, and again, in order, from the base class to the derived class; The deconstruction order is just the opposite;
The code is as follows:
* * * cppprimer.cpp * * Created on:2014.1.10 * author:spike//*eclipse CDT, gcc 4.8.1*/ #include <iostream> #include <string> class Zooanimal {public:zooanimal () {St
D::cout << "Zooanimal constructor!" << Std::endl;
} void Zooprint (void) {std::cout << "Zoo animal!" << Std::endl;}
}; Class Bear:public virtual Zooanimal {public:bear () {std::cout << "bear constructor!" << ;
Std::endl;
} void Bearprint (void) {std::cout << "bear!" << Std::endl;}
};
Class Character {Public:character () {std::cout << "Character constructor!" << Std::endl;
}
}; Class Bookcharacter:public Character {Public:bookcharacter () {std::cout << "Bookcharacter Co
nstructor! "<< Std::endl;
}
};
Class Toyanimal {public: Toyanimal () {std::cout << "toyanimal constructor!" << Std::endl;
}
}; Class Teddybear:public Bookcharacter, public bear, public virtual Toyanimal {Publ
Ic:teddybear () {std::cout << "teddybear constructor!" << Std::endl;
}
};
int main (void) {teddybear TB; }
Output:
Zooanimal constructor!
Toyanimal constructor!
Character constructor!
Bookcharacter constructor!
Bear constructor!
Teddybear constructor!
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/