< previous page page_299 next page >

Page 299
Summary
In this hour, you learned how to use the dynamic_cast operator to cast down the inheritance hierarchy. You also learned why this may be a sign of poor class design.
You also saw how to create abstract data types using pure virtual functions and how to implement the pure virtual functions so that they can be used by derived classes.
Q&A
Q What does percolating functionality upward mean?
A This refers to the idea of moving shared functionality upwards into a common base class. If more than one class shares a function, it is desirable to find a common base class in which that function can be stored.
Q Is percolating upward always a good thing?
A Yes, if you are percolating shared functionality upward; no, if all you are moving is interface. That is, if all the derived classes can't use the method, it is a mistake to move it up into a common base class. If you do, you'll have to switch on the runtime type of the object before deciding whether you can invoke the function.
Q Why is dynamic casting bad?
A The point of virtual functions is to let the virtual table, rather than the programmer, determine the runtime type of the object.
Q Why bother making an abstract data type? Why not just make it non-abstract and avoid creating any objects of that type?
A The purpose of many of the conventions in C++ is to enlist the compiler in finding bugs, so as to avoid runtime bugs in code that you give your customers. Making a class abstractthat is, giving it pure virtual functionscauses the compiler to flag any objects created of that abstract type as errors.

 
< previous page page_299 next page >

If you like this book, buy it!