13
Virtual Constructors and Factories
In C++, any member function of any class, including its destructor, can be made virtual - any member function except one—the constructor. Without virtual functions, the exact type of object on which the member function is invoked is known at compile time. Therefore, the type of object that is constructed is always known at compile time, at the point of the constructor call. Nonetheless, we often need to construct objects whose type is not known until runtime. This chapter describes several related patterns and idioms that address this design problem in various ways, including the Factory pattern.
The following topics will be covered in this chapter:
- Why there is no way to make a constructor virtual
- How ...
Get Hands-On Design Patterns with C++ - Second Edition 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.