FAQ 21.13 Can an ABC have a pure virtual destructor?

Yes, provided the ABC (abstract base class) gives an explicit definition elsewhere. An example follows.

 class Base { public:   Base() throw();   virtual ~Base() throw() = 0; }; inline Base::Base()  throw() { } inline Base::~Base() throw() { }                     <-- 1 class Derived : public Base { public:   Derived() throw(); }; Derived::Derived() throw() : Base() { } int main() { Derived d; } 

(1) Defined even though pure virtual

Leaving out a definition for Base::~Base() will cause a linker error, because Derived::~Derived() automatically calls Base::~Base() (see FAQ 20.05). In this case, Derived::~Derived() is synthesized by the compiler.

Depending on the compiler, there may be a marginal performance benefit in using a pure virtual destructor with an explicit inline definition versus the inline virtual technique that was described in the previous FAQ. Calls to inline virtual functions can be inlined if the compiler is able to statically bind to the class. However, the compiler may also make an outlined copy of an inline virtual function (for any other cases where it isn't able to statically bind to the call). Although in theory destructors of ABCs don't have these limitations, in practice not all compilers produce optimal code when using the inline virtual technique.



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