虚继承(virtual inheritance)的构造(construct)和析构(destruct)顺序
在多重继承的时候, 构造顺序是 虚继承按顺序优先, 再次是其他按顺序, 由基类至派生类; 析构顺序正好相反;
代码如下:
/* * cppprimer.cpp * * Created on: 2014.1.10 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <string> class ZooAnimal { public: ZooAnimal () { std::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 Constructor!" << std::endl; } }; class ToyAnimal { public: ToyAnimal () { std::cout << "ToyAnimal Constructor!" << std::endl; } }; class TeddyBear : public BookCharacter, public Bear, public virtual ToyAnimal { public: TeddyBear () { std::cout << "TeddyBear Constructor!" << std::endl; } }; int main (void) { TeddyBear TB; }
输出:
ZooAnimal Constructor! ToyAnimal Constructor! Character Constructor! BookCharacter Constructor! Bear Constructor! TeddyBear Constructor!
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c++
, include
, 顺序
, construct
, 多重
Inheritance
construct destruct、析构函数 继承、c 多继承 析构函数、c 继承 析构函数、php construct 继承,以便于您获取更多的相关知识。
时间: 2024-11-05 00:14:21