Copy constructor needs to call a method that depends on the object, but constructor can't be virtual

后端 未结 6 629
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 19:24

I have an abstract base class with two inherited classes. In both these classes I define a virtual method that is used by the constructor. Now I need to create a copy constr

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 20:20

    There is a nice discussion of the Prototype Pattern here which could be helpful.

    Prototype [Go4]

    Problem

    A "factory" class can't anticipate the type of "product" objects it must create.

    Solution

    Derive all product classes from an abstract Product base class that declares a pure virtual clone() method. The Product base class also functions as the product factory by providing a static factory method called makeProduct(). This function uses a type description parameter to locate a prototype in a static prototype table maintained by the Product base class. The prototype clones itself, and the clone is returned to the caller.

    The reference to 'Go4' is for the seminal book on Design Patterns written by the "Gang of Four" authors.

提交回复
热议问题