Best method to implement an abstract factory pattern

前端 未结 2 1910
醉酒成梦
醉酒成梦 2021-01-20 05:15

Consider following code:

#include 


// =============================


class Shape{
public:
    virtual ~Shape(){};

    virtual void process         


        
2条回答
  •  遇见更好的自我
    2021-01-20 06:07

    I think the most idiomatic modern C++ method is the one you mention in passing but ignore. Return a std::unique_ptr.

    It is safe, clearly expresses ownership, supports polymorphism and doesn't need much code.

    class ShapeFactory {
    public:
      std::unique_ptr create(){
        return std::make_unique();
      }
    };
    

    But I wouldn't want to claim it was the "best" method.

    Your PimplShape in option 3 is actually very similar to a unique_ptr just less generic or tested.

提交回复
热议问题