Firstly, this is crap code because of that cast.
But look at this line:
-
pBClass = new CDerivedClass(*(CDerivedClass *)pPtrToBaseClass);
-
Take out the cast and you have:
-
pBClass = new CDerivedClass(*pPtrToBaseClass);
-
Dereferencing a pointer to a base class object gives you the base class object itself. So, what they did was typecast the pointer to base class to a derived class pointer. By de-referencing that, you have a derived object. So the code becomes:
-
pBClass = new CDerivedClass(CDerivedClass);
-
So it is, in fact a copy constructor call.
That stupid cast
assumes the base class pointer is pointing a a CDerived object. If it's not, like there are several kinds of derived classes, then this cast will be to the incorrect type and your program will crash right here.
The code should be:
-
CDerivedClass pPtrToDerivedClass = new CDerivedClass
-
pBClass = new CDerivedClass(*pPtrToDerivedClass);
-
You cannot create a derived object by using a pointer to a base object. The base object doesn't know that there are derived objects.