![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
设计模式
写点C++的设计模式
恩嗯
一个正在学习的,普通人
展开
-
工厂模式
简单工厂模式: #include<iostream> using namespace std; class Fruits { public: Fruits(const string &name) :nname(name){} virtual void Show() = 0; private: string nname; }; class Apple: public Fruits { public: Apple(const st原创 2020-07-20 22:09:18 · 101 阅读 · 0 评论 -
不能被继承的类
#include<iostream> /* 通过友缘和虚继承实现。 在继承层次中,先构造虚基类,可是虚基类A,只能由B来进行构造,也就是说B这个类不能被继承 注意B的声明必须放到A的前面。 */ using namespace std; class B; class A { private: A() {}; friend class B; }; class B : virtual public A { public: B():A() {} };原创 2020-07-20 22:05:20 · 176 阅读 · 0 评论 -
单例模式
饿汉模式 /* #include<iostream> using namespace std; class Test { public: static Test* Getinstance()//只能以指针返回,其他不行。static调用约定不是thiscall { return m_instance; } private: Test()//将构造和拷贝构造写在私有下 {}; Test(const Test & r原创 2020-07-16 22:05:29 · 83 阅读 · 0 评论