11.12. Virtual Constructors

You have seen the impact that virtual functions have on application designs. With dynamic binding, methods resolve at run time, easing maintenance when you add new classes and behaviors to inheritance hierarchies. This late-binding mechanism (virtual) applies to any class member function and destructor.

Not all class member functions are dynamically bound, however. A constructor, for example, may not be virtual. Why not? Recall that a constructor's primary task is to convert raw memory into initialized objects. A constructor must also build tables of virtual function pointers for dynamic binding (see Figure 11.14 on page 557). Since a “virtual” table for an object cannot exist before the compiler calls a class constructor, ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.