From d8c51db31c38b36350406dd32a689de8d0ffaf3c Mon Sep 17 00:00:00 2001 From: huihut Date: Wed, 21 Mar 2018 17:00:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E4=BE=8B=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E6=94=B9=E6=AD=A3=E7=B1=BB=E5=90=8E=E9=9D=A2?= =?UTF-8?q?=E7=9A=84=E9=80=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 19984e2..2a06b60 100644 --- a/README.md +++ b/README.md @@ -415,7 +415,7 @@ class A public: void do(int a); void do(int a, int b); -} +}; ``` #### 动态多态(晚绑定) @@ -437,19 +437,19 @@ public: { ... } -} +}; class Circle : public Shape //圆形类 { public: virtual double calcArea(); ... -} +}; class Rect : public Shape //矩形类 { public: virtual double calcArea(); ... -} +}; int main() { Shape * shape1 = new Circle(4.0); @@ -469,13 +469,13 @@ public: Shape(); //构造函数不能是虚函数 virtual double calcArea(); virtual ~Shape(); //虚析构函数 -} +}; class Circle : public Shape //圆形类 { public: virtual double calcArea(); ... -} +}; int main() { Shape * shape1 = new Circle(4.0); @@ -581,21 +581,21 @@ class Flyable //【能飞的】 public: virtual void takeoff() = 0; // 起飞 virtual void land() = 0; // 降落 -} +}; class Bird : public Flyable //【鸟】 { public: void foraging() {...} // 觅食 virtual void takeoff() {...} virtual void land() {...} -} +}; class Plane : public Flyable //【飞机】 { public: void carry() {...} // 运输 virtual void take off() {...} virtual void land() {...} -} +}; class type_info { @@ -607,7 +607,7 @@ public: virtual ~type_info(); private: ... -} +}; class doSomething(Flyable *obj) //【做些事情】 { @@ -622,7 +622,7 @@ class doSomething(Flyable *obj) //【做些事情】 } obj->land(); -} +}; ``` dynamic\_cast 注意事项: @@ -1444,6 +1444,26 @@ ssize_t write(int fd, const void *buf, size_t count); ## 设计模式 +### 单例模式 + +```cpp +class CSingleton +{ +private: + CSingleton() + { + } + static CSingleton *m_pInstance; +public: + static CSingleton * GetInstance() + { + if(m_pInstance == nullptr) + m_pInstance = new CSingleton(); + return m_pInstance; + } +}; +``` + ## 链接装载库 ### 内存、栈、堆