From f98a4ce2c23c11a2bd19d13f6bc80ccb777ddff0 Mon Sep 17 00:00:00 2001 From: Light-City <455954986@qq.com> Date: Sun, 8 Dec 2019 21:09:59 +0800 Subject: [PATCH] update --- README.md | 23 ++++-- morden_C++_30/CMakeLists.txt | 7 ++ morden_C++_30/RAII/RAII.cpp | 96 ++++++++++++++++++++++++ morden_C++_30/RAII/heap_stack.cpp | 38 ++++++++++ {stl_src => stl}/array.md | 0 {stl_src => stl}/deque.md | 0 {stl_src => stl}/hashtable.md | 0 {stl_src => stl}/iterator.md | 0 {stl_src => stl}/list.md | 0 {stl_src => stl}/map_multimap.md | 0 {stl_src => stl}/myhashtable.md | 0 {stl_src => stl}/queue_stack.md | 0 {stl_src => stl}/rb_tree.md | 0 {stl_src => stl}/set_multiset.md | 0 {stl_src => stl}/traits.md | 0 {stl_src => stl}/typename.md | 0 {stl_src => stl}/unordered_map.md | 0 {stl_src => stl}/vector.md | 0 {stl_src => stl}/谈谈STL设计之EBO优化.md | 0 19 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 morden_C++_30/CMakeLists.txt create mode 100644 morden_C++_30/RAII/RAII.cpp create mode 100644 morden_C++_30/RAII/heap_stack.cpp rename {stl_src => stl}/array.md (100%) rename {stl_src => stl}/deque.md (100%) rename {stl_src => stl}/hashtable.md (100%) rename {stl_src => stl}/iterator.md (100%) rename {stl_src => stl}/list.md (100%) rename {stl_src => stl}/map_multimap.md (100%) rename {stl_src => stl}/myhashtable.md (100%) rename {stl_src => stl}/queue_stack.md (100%) rename {stl_src => stl}/rb_tree.md (100%) rename {stl_src => stl}/set_multiset.md (100%) rename {stl_src => stl}/traits.md (100%) rename {stl_src => stl}/typename.md (100%) rename {stl_src => stl}/unordered_map.md (100%) rename {stl_src => stl}/vector.md (100%) rename {stl_src => stl}/谈谈STL设计之EBO优化.md (100%) diff --git a/README.md b/README.md index 2ef1b58..9c55347 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # C++那些事 -## 项目概要 +### 0.项目概要 -学习C++内容,包括理论、源码、实践、项目等 +学习C++内容,包括理论、源码、实践、课程代码、项目等 ### 1.基础部分 @@ -127,12 +127,25 @@ for(decl:col) { - [myhashtable](./stl_src/myhashtable.md) - [unordered_map](./stl_src/unordered_map.md) +### 3.学习课程 -### 3.代码运行 +#### 3.1 [极客时间《现代C++实战30讲》](https://time.geekbang.org/channel/home) -全部在Ubuntu18.04下用vim编写,使用gcc/g++调试!全部可正常运行! +- [堆、栈、RAII:C++里该如何管理资源?](./morden_C++_30) + - [堆与栈](./morden_C++_30/RAII/heap_stack.cpp) + - [RAII](./morden_C++_30/RAII/RAII.cpp) -## 关于作者: +### 4.代码运行 + +- **代码环境** + +Ubuntu 18.04 + +- **工具** + +CLion gcc/g++ + +### 5.关于作者: 个人公众号: diff --git a/morden_C++_30/CMakeLists.txt b/morden_C++_30/CMakeLists.txt new file mode 100644 index 0000000..89ccccb --- /dev/null +++ b/morden_C++_30/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.14) +project(Morden_C++) + +set(CMAKE_CXX_STANDARD 11) + +add_executable(heap_stack_RAII RAII/heap_stack.cpp) +add_executable(RAII RAII/RAII.cpp) \ No newline at end of file diff --git a/morden_C++_30/RAII/RAII.cpp b/morden_C++_30/RAII/RAII.cpp new file mode 100644 index 0000000..4a4e932 --- /dev/null +++ b/morden_C++_30/RAII/RAII.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +using namespace std; + +enum class shape_type { + circle, + triangle, + rectangle, +}; + + +class shape { +public: + shape() { cout << "shape" << endl; } + + virtual void print() { + cout << "I am shape" << endl; + } + + virtual ~shape() {} +}; + +class circle : public shape { +public: + circle() { cout << "circle" << endl; } + + void print() { + cout << "I am circle" << endl; + } +}; + +class triangle : public shape { +public: + triangle() { cout << "triangle" << endl; } + + void print() { + cout << "I am triangle" << endl; + } +}; + +class rectangle : public shape { +public: + rectangle() { cout << "rectangle" << endl; } + + void print() { + cout << "I am rectangle" << endl; + } +}; + +// 利用多态 上转 如果返回值为shape,会存在对象切片问题。 +shape *create_shape(shape_type type) { + switch (type) { + case shape_type::circle: + return new circle(); + case shape_type::triangle: + return new triangle(); + case shape_type::rectangle: + return new rectangle(); + } +} + +class shape_wrapper { +public: + explicit shape_wrapper(shape *ptr = nullptr) : ptr_(ptr) {} + + ~shape_wrapper() { + delete ptr_; + } + + shape *get() const { + return ptr_; + } + +private: + shape *ptr_; +}; + + +void foo() { + shape_wrapper ptr(create_shape(shape_type::circle)); + ptr.get()->print(); +} + +int main() { + + // 第一种方式 + shape *sp = create_shape(shape_type::circle); + sp->print(); + delete sp; + + // 第二种方式 + foo(); + + return 0; +} diff --git a/morden_C++_30/RAII/heap_stack.cpp b/morden_C++_30/RAII/heap_stack.cpp new file mode 100644 index 0000000..893c7d1 --- /dev/null +++ b/morden_C++_30/RAII/heap_stack.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +class bar { + +}; + +// java 程序员风格 +void foo() { + cout << "method 1" << endl; + bar *ptr = new bar(); + delete ptr; +} + +bar *make_bar() { + bar *ptr = nullptr; + try { + ptr = new bar(); + } catch (...) { + delete ptr; + throw; + } + return ptr; +} +// 独立出函数 分配和释放不在一个函数里 +void foo1() { + cout << "method 2" << endl; + bar *ptr = make_bar(); + delete ptr; +} + + +int main() { + foo(); + foo1(); + return 0; +} \ No newline at end of file diff --git a/stl_src/array.md b/stl/array.md similarity index 100% rename from stl_src/array.md rename to stl/array.md diff --git a/stl_src/deque.md b/stl/deque.md similarity index 100% rename from stl_src/deque.md rename to stl/deque.md diff --git a/stl_src/hashtable.md b/stl/hashtable.md similarity index 100% rename from stl_src/hashtable.md rename to stl/hashtable.md diff --git a/stl_src/iterator.md b/stl/iterator.md similarity index 100% rename from stl_src/iterator.md rename to stl/iterator.md diff --git a/stl_src/list.md b/stl/list.md similarity index 100% rename from stl_src/list.md rename to stl/list.md diff --git a/stl_src/map_multimap.md b/stl/map_multimap.md similarity index 100% rename from stl_src/map_multimap.md rename to stl/map_multimap.md diff --git a/stl_src/myhashtable.md b/stl/myhashtable.md similarity index 100% rename from stl_src/myhashtable.md rename to stl/myhashtable.md diff --git a/stl_src/queue_stack.md b/stl/queue_stack.md similarity index 100% rename from stl_src/queue_stack.md rename to stl/queue_stack.md diff --git a/stl_src/rb_tree.md b/stl/rb_tree.md similarity index 100% rename from stl_src/rb_tree.md rename to stl/rb_tree.md diff --git a/stl_src/set_multiset.md b/stl/set_multiset.md similarity index 100% rename from stl_src/set_multiset.md rename to stl/set_multiset.md diff --git a/stl_src/traits.md b/stl/traits.md similarity index 100% rename from stl_src/traits.md rename to stl/traits.md diff --git a/stl_src/typename.md b/stl/typename.md similarity index 100% rename from stl_src/typename.md rename to stl/typename.md diff --git a/stl_src/unordered_map.md b/stl/unordered_map.md similarity index 100% rename from stl_src/unordered_map.md rename to stl/unordered_map.md diff --git a/stl_src/vector.md b/stl/vector.md similarity index 100% rename from stl_src/vector.md rename to stl/vector.md diff --git a/stl_src/谈谈STL设计之EBO优化.md b/stl/谈谈STL设计之EBO优化.md similarity index 100% rename from stl_src/谈谈STL设计之EBO优化.md rename to stl/谈谈STL设计之EBO优化.md