Replacement in C++ - issues

Let us see how replacement works in our example languages. Replacment in C++ is tricky. If a method is declared using the virtual keyword, then the designer of the parent class is stating that they expect it might be replaced. In such a case a child class can override the method.

But the polymorphic behavior we expect will only occur if a variable is declared as a pointer or a reference. An ordinary variable will not use dynamic binding on a method, even if it is declared as virtual.

The flip side is also possible. If a method is not declared as virtual it does not mean that it cannot be overridden. But if it is overridden, the selection of the method will still be made on the static, or declared type of a variable, and not on the dynamic variable value.

To confuse matters even further, constructors always use refinement semantics, other methods use replacement semantics. We will discuss this more when we investigate refinement.

[audio] [real] Text to accompany slide7, in Chapter 11 of An Introduction to Object-Oriented Programming