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 the object on which the member function is invoked is known at compile time. Therefore, the type of the 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 to use the Factory pattern to defer the choice of the constructed object type until compile time

  • Using C++ idioms to construct and copy objects polymorphically

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.40.4