At run time, we may need to clone an object that is passed by pointer. This is illustrated in listing 2.
Listing 2: | cloneme |
However, we have a problem here. The creation of the object pointed to by pClone uses a regular cloning (copy) constructor. Because the type pointed to by pA is class A, we cannot use any other constructors. As a result, the resulting clone is a genuine class A object, but nothing more. Note that pA can point to an object of class B.
This means that when m1 is invoked, the clone invokes A::m1, even though pA->m1() would have invoked B::m1. There are very few, if any, cases that we want a clone to revert to a super close instead of the class of the original object.
Fortunately, there is a way to get around the limitations of the copy constructor method.