Home Segments Top Top Previous Next

533: Sidetrip

In C++, you are allowed to create arrays of class instances, as well as arrays of pointers to class instances. If you have an array of class instances, all the elements must be instances of the class, rather than instances of the class's subclasses, because only enough room is reserved for instances of the class itself.

Accordingly, if you want to place subclass instances in an array, you must use a pointer
array, which, in C++, requires you to understand pointer dereferencing and virtual
functions.

By uniformly implementing reference arrays with automatically handled pointers, Java avoids the need for both pointer dereferencing and virtual functions.