Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
319
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

b.push_back(new X);

for(int i = 0; i < b.size(); i++) cout << b[i]->vf() << endl;

purge(b); } ///:~

If you can always arrange for a virtual base class to have a default constructor, you’ll make things much easier for anyone who inherits from that class.

Overhead

The term “pointer magic” has been used to describe the way virtual inheritance is implemented. You can see the physical overhead of virtual inheritance with the following program:

//: C06:Overhead.cpp

// Virtual base class overhead #include <fstream>

using namespace std;

ofstream out("overhead.out");

class MBase { public:

virtual void f() const {}; virtual ~MBase() {}

};

class NonVirtualInheritance : public MBase {};

class VirtualInheritance

: virtual public MBase {};

class VirtualInheritance2

: virtual public MBase {};

class MI

:public VirtualInheritance, public VirtualInheritance2 {};

#define WRITE(ARG) \

out << #ARG << " = " << ARG << endl;

Chapter 15: Multiple Inheritance

351

Соседние файлы в предмете Программирование