21.9 Answers to Chapter Questions

I l @ ve RuBoard

Answer 21-1: Remember that destructors are called in the order derived class first, then base class. In this case, the destructor for the derived class, list_of_integers , is called to destroy the class. The class is gone.

Next, the destructor for the base class list is called. It calls the function clear . This is a pure virtual function, so C++ must call the clear function in the derived class. But the derived class isgone. There is no clear function. This makes C++ very upset and it aborts the program. (Actually, only good compilers will cause a program to abort. Others may do something really strange to your program.)

You should never call pure virtual functions from a destructor. Actually, be very careful when you call virtual functions of any type in a destructor.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

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