leo*_*leo 3 c++ design-patterns class
我有一个System类,可以返回指向Editor类的指针.该编辑器类的实例化内系统类,并通过指针系统的私有变量.所述编辑器类实质上充当备用接口系统的内部数据结构.
我的问题:是否存在允许我禁止直接实例化Editor类的设计模式,但仍以某种方式在System中实例化它?
谢谢.
你可以使Editor的构造函数私有,这将使其他人不能实例化它,然后让System成为朋友将允许它访问构造函数.
class System {
public:
    System() : editor_(new Editor()) { ... }
private:
    Editor* editor_;
}
class Editor {
    friend class System;
    Editor() { ... }
}