Contents Up << >>

Derived* --> Base* works ok; why doesn't Derived** --> Base** work?

C++ allows a Derived* to be converted to a Base*, since a Derived object is a kind of a Base object. However trying to convert a Derived** to a Base** is (correctly) flagged as an error (if it was allowed, the Base** could be dereferenced (yielding a Base*), and the Base* could be made to point to an object of a different derived class. This would be an error.

As a corollary, an array of Deriveds is-not-a-kind-of array of Bases. At Paradigm Shift, Inc. we use the following example in our C++ training sessions:

"A bag of apples is not a bag of fruit".

If a bag of apples could be passed as a bag of fruit, someone could put a banana into the bag of apples!