原型模式

#include <memory>
#include <iostream>

class Prototype {
public:
    Prototype() {}
    Prototype(const Prototype& p) { this->data = p.data; }
    virtual std::unique_ptr<Prototype> copy() { return std::make_unique<Prototype>(*this); }
    void show() { std::cout << "In Prototype show(). The data is " << data << std::endl; }
    void set(int data) { this->data = data; }
private:
    int data = 0;
};

int main(int argc, char* argv[]) {
    Prototype p;
    std::unique_ptr<Prototype> up = p.copy();
    up->show();
    return 1;
}

  

posted @ 2022-02-25 17:46  南乡水  阅读(13)  评论(0编辑  收藏  举报