When does an object get its type?

It is pretty easy to understand why the process that we described previously cannot work for creating virtual constructors. First of all, it is evident from the description of the preceding process—as a part of it, we identify the type that the object was created with. That can only happen after the object is constructed—before construction, we don't have an object of this type yet, just some uninitialized memory. Another way to look at it is this—before the virtual function is dispatched to the correct type, the v-pointer needs to be looked up. Who puts the right value into the v-pointer? Considering that the v-pointer uniquely identifies the type of the object, it can only be initialized during construction. ...

Get Hands-On Design Patterns with C++ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.