FAQ 21.12 How should a virtual destructor be defined when it has no code?

It should normally be defined as an inline virtual function. An example follows.

 #include <iostream> using namespace std; class Base { public:   virtual void f() throw();   virtual void g() throw();   virtual ~Base()  throw(); }; void Base::f() throw() { cout << "Base::f()\n"; } void Base::g() throw() { cout << "Base::g()\n"; } inline Base::~Base()  throw() { } class Derived : public Base { public:   virtual void f() throw(); }; void Derived::f() throw() { cout << "Derived::f()\n";  } int main() {   Base b;                                            <-- 1   b.f();   b.g();   Derived d;   d.f();   d.g(); } 

(1) OK: Base is not an ABC

The reason Base::~Base() is inline is to avoid an unnecessary function call when Derived::~Derived() automatically calls Base::~Base() (see FAQ 20.05). In this case, Derived::~Derived() is synthesized by the compiler.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net