diff --git a/.gitignore b/.gitignore index 82c5d68e..97497e0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ com.sh +bazel-CPlusPlusThings +bazel-bin +bazel-out +bazel-testlogs +bazel-cplusplus_bazel +.vscode \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..7f421c08 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,112 @@ +{ + "files.associations": { + "iostream": "cpp", + "ostream": "cpp", + "__availability": "cpp", + "__config": "cpp", + "__string": "cpp", + "deque": "cpp", + "filesystem": "cpp", + "functional": "cpp", + "iterator": "cpp", + "ratio": "cpp", + "__bit_reference": "cpp", + "__bits": "cpp", + "__debug": "cpp", + "__errc": "cpp", + "__functional_base": "cpp", + "__hash_table": "cpp", + "__locale": "cpp", + "__mutex_base": "cpp", + "__node_handle": "cpp", + "__nullptr": "cpp", + "__split_buffer": "cpp", + "__threading_support": "cpp", + "__tree": "cpp", + "__tuple": "cpp", + "algorithm": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "queue": "cpp", + "regex": "cpp", + "set": "cpp", + "sstream": "cpp", + "stack": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "utility": "cpp", + "vector": "cpp", + "any": "cpp", + "barrier": "cpp", + "*.tcc": "cpp", + "cfenv": "cpp", + "charconv": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "concepts": "cpp", + "coroutine": "cpp", + "csetjmp": "cpp", + "csignal": "cpp", + "cuchar": "cpp", + "forward_list": "cpp", + "memory_resource": "cpp", + "random": "cpp", + "source_location": "cpp", + "future": "cpp", + "latch": "cpp", + "numbers": "cpp", + "ranges": "cpp", + "scoped_allocator": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "span": "cpp", + "stop_token": "cpp", + "syncstream": "cpp", + "thread": "cpp", + "typeindex": "cpp", + "valarray": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 71dbbd21..7ffe6e78 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,61 @@ + +- [C++ 那些事](#c-那些事) + - [重磅项目](#重磅项目) + - [关于作者](#关于作者) + - [项目运行](#项目运行) + - [方式1: vscode + bazel](#方式1-vscode--bazel) + - [方式2: docker](#方式2-docker) + - [方式3: g++](#方式3-g) + - [视频学习本项目](#视频学习本项目) + - [飞书知识库](#飞书知识库) + - [基础进阶](#基础进阶) + - [实战系列](#实战系列) + - [10 日狂练](#10-日狂练) + - [重点实战练习](#重点实战练习) + - [C++2.0 新特性](#c20-新特性) + - [概况](#概况) + - [C++11 新特性](#c11-新特性) + - [C++14/17/20](#c141720) + - [设计模式](#设计模式) + - [STL 源码剖析](#stl-源码剖析) + - [并发编程](#并发编程) + - [C++ Concurrency in Action](#c-concurrency-in-action) + - [多线程与多进程](#多线程与多进程) + - [Threading In C++](#threading-in-c) + - [C++ 惯用法](#c-惯用法) + - [你最喜欢的 c++ 编程风格惯用法是什么?](#你最喜欢的-c-编程风格惯用法是什么) + - [学习课程](#学习课程) + - [极客时间《现代 C++ 实战 30 讲》](#极客时间现代-c-实战-30-讲) + - [工具篇](#工具篇) + - [容器快捷输出工具](#容器快捷输出工具) + - [像 Python 一样简单输出(Jupyter Notebook)](#像-python-一样简单输出jupyter-notebook) + - [观察编译过程变化](#观察编译过程变化) + - [C++ 的 Debug 工具 dbg-macro](#c-的-debug-工具-dbg-macro) + - [在 Linux 上的 Debug 工具 rr - 拥有回到过去的能力](#在-linux-上的-debug-工具-rr---拥有回到过去的能力) + - [拓展部分](#拓展部分) + - [一些问题](#一些问题) + - [贡献者](#贡献者) + # C++ 那些事 -[English version is here](./README_EN.md) +感谢各位对《C++ 那些事》的支持,现将内容也同步至B站,以视频的方式展示,可以打开网站直接阅读~欢迎大家 star、转发、PR。 -感谢各位对《C++ 那些事》的支持,现将内容也同步至网站,可以打开网站直接阅读~欢迎大家 star、转发、PR。 +在线个人博客:[光城的实验室](https://light-city.github.io/) -因服务器到期,暂时关闭以下站点,请大家阅读仓库即可,感谢支持。 +在线学习网站:[C++那些事](https://light-city.github.io/stories_things/) +- 中文名:**C++ 那些事** +- 英文名:**Stories About C Plus Plus** -> ~~[直通点](https://light-city.club/sc/)~~ +这是一个适合初学者从**入门到进阶**的仓库,解决了**面试者与学习者**想要**深入 C++** 及如何**入坑 C++** 的问题。除此之外,本仓库拓展了更加深入的源码分析,多线程并发等的知识,是一个比较全面的 C++ 学习从入门到进阶提升的仓库。 -## 规划 -将会使用bazel重构整个项目架构,方便编译~ +### 重磅项目 +推出一系列重磅项目,带大家实战学习C++,配合本开源项目学习,成长飞快! +直通点:[戳这里](./proj/README.md) ### 关于作者 @@ -22,42 +63,63 @@ 个人公众号:guangcity -![](img/wechat.jpg) - -欢迎大家反馈及 C++ 交流,本人在微信创建了 C++ 那些事交流群,优质的 C++ 资源交流区,期待大家关注上面公众号,点击公众号右下角->联系我,拉你入群。 +或者扫下面的二维码,欢迎大家反馈及 C++ 交流,本人在微信创建了 C++ 那些事交流群,优质的 C++ 资源交流区,期待大家关注上面公众号,点击公众号右下角->联系我,拉你入群。 --- +>关注我 -另外,下面是我自己建立的知识星球,会带大家做一些小项目以及源码解读等方面成长。 +如果觉得不错,关注我吧~ -![](https://www.hualigs.cn/image/60a24728bb736.jpg) + + + + + + + +
+
+ 星球 +
+
+ 微信公众号 +
### 项目运行 -- **代码环境** - Ubuntu 18.04 +#### 方式1: vscode + bazel -- **编辑工具** +#### 方式2: docker - CLion +新推出免开发环境,docker环境,大家可以pull下面镜像: -- **编译器** +``` +docker pull xingfranics/cplusplusthings:latest +``` +#### 方式3: g++ -> gcc/g++ -尽量用 g++,因为一开始我用的 vim 写的,所以纯 g++,后来用编译器 CLion,部分有 CMakeLists.txt 文件,直接导入就行,后面会更新这个文件包含整个项目,持续关注~ +### 视频学习本项目 -### 0.项目概要 +[第一期:Step By Step上手编译本项目](https://www.bilibili.com/video/BV1Rv4y1H7LB/?vd_source=bb6532dcd5b1d6b26125da900adb618e) -- 中文名:**C++ 那些事** -- 英文名:**Stories About C Plus Plus** +[第二期:免开发环境docker](https://www.bilibili.com/video/BV1oz4y1a7Pu/?vd_source=bb6532dcd5b1d6b26125da900adb618e) -这是一个适合初学者从**入门到进阶**的仓库,解决了**面试者与学习者**想要**深入 C++** 及如何**入坑 C++** 的问题。除此之外,本仓库拓展了更加深入的源码分析,多线程并发等的知识,是一个比较全面的 C++ 学习从入门到进阶提升的仓库。 +[第三期:手把手共读HashTable,彻底搞懂C++ STL](https://www.bilibili.com/video/BV1o8411U7vy/?vd_source=bb6532dcd5b1d6b26125da900adb618e) + +[第四期:手把手共读STL之enable_shared_from_this](https://www.bilibili.com/video/BV1du4y1w7Mg/?spm_id_from=333.788&vd_source=bb6532dcd5b1d6b26125da900adb618e) + +### 飞书知识库 + +[互联网大厂面试实录](https://hmpy6adnp5.feishu.cn/docx/OitBdRB4KozIhTxQt7Ec7iFDnkc) + +[拿下offer之必备面经](https://hmpy6adnp5.feishu.cn/docx/B1aCdVTUgoyJGYxtWV7cdvgRnxv) -### 1.基础进阶 + +### 基础进阶 - [`const` 那些事](./basic_content/const) - [`static` 那些事](./basic_content/static) @@ -85,85 +147,72 @@ - [引用与指针那些事](./basic_content/pointer_refer) - [宏那些事](./basic_content/macro) -### 2.实战系列 - -#### 2.1 [10 日狂练](./practical_exercises/10_day_practice) - -- [x] [day1](practical_exercises/10_day_practice/day1) +### 实战系列 -基本 c++ 语法 +#### [10 日狂练](./practical_exercises/10_day_practice) -- [x] [day2](practical_exercises/10_day_practice/day2) +- [day1-基本语法](practical_exercises/10_day_practice/day1) -递归、结构体、枚举、静态变量等 +- [day2-递归、结构体、枚举、静态变量等](practical_exercises/10_day_practice/day2) -- [x] [day3](practical_exercises/10_day_practice/day3) +- [day3-函数1](practical_exercises/10_day_practice/day3) -函数 +- [day4-函数2](practical_exercises/10_day_practice/day4) -- [x] [day4](practical_exercises/10_day_practice/day4) +- [day5-继承多态](practical_exercises/10_day_practice/day5) -函数深入 +- [day6-虚函数与抽象类](practical_exercises/10_day_practice/day6) -- [x] [day5](practical_exercises/10_day_practice/day5) +- [day7-运算符重载](practical_exercises/10_day_practice/day7) -继承多态 +- [day8-模版与STL](practical_exercises/10_day_practice/day8) -- [x] [day6](practical_exercises/10_day_practice/day6) +- [day9-异常](practical_exercises/10_day_practice/day9) -虚函数、抽象类 +- [day10-文件与流](practical_exercises/10_day_practice/day10) -- [x] [day7](practical_exercises/10_day_practice/day7) +#### [重点实战练习](./practical_exercises/key_exercises/) -运算符重载 +- [中括号重载.cpp](./practical_exercises/key_exercises/bracket_overloading.cpp) -- [x] [day8](practical_exercises/10_day_practice/day8) +- [时钟++运算符重载.cpp](./practical_exercises/key_exercises/clock.cpp) -模板与 STL +- [运算符重载之强制转换.cpp](./practical_exercises/key_exercises/operator_cast.cpp) -- [x] [day9](practical_exercises/10_day_practice/day9) +- [重载圆括号的时钟.cpp](./practical_exercises/key_exercises/operator_circle.cpp) -异常 +- [函数模板.cpp](./practical_exercises/key_exercises/func_temp.cpp) -- [x] [day10](practical_exercises/10_day_practice/day10) +- [动态数组.cpp](./practical_exercises/key_exercises/array.cpp) -文件与流 +- [字典插入与查找.cpp](./practical_exercises/key_exercises/map_insert_look.cpp) -#### 2.2 [重点实战练习](./practical_exercises/key_exercises/) +- [异常捕获.cpp](./practical_exercises/key_exercises/try.cpp) -├── [中括号重载.cpp](./practical_exercises/key_exercises/中括号重载.cpp) -├── [时钟++运算符重载.cpp](./practical_exercises/key_exercises/时钟++运算符重载.cpp) -├── [运算符重载之强制转换.cpp](./practical_exercises/key_exercises/运算符重载之强制转换.cpp) -└── [重载圆括号的时钟.cpp](./practical_exercises/key_exercises/重载圆括号的时钟.cpp) +- [类模板之栈.cpp](./practical_exercises/key_exercises/stack.cpp) -├── [函数模板.cpp](./practical_exercises/key_exercises/函数模板.cpp) +- [类模板特化之数组.cpp](./practical_exercises/key_exercises/array_template.cpp) -├── [动态数组.cpp](./practical_exercises/key_exercises/动态数组.cpp) +- [继承与封装.cpp](./practical_exercises/key_exercises/override.cpp) -├── [字典插入与查找.cpp](./practical_exercises/key_exercises/字典插入与查找.cpp) +- [读写文件综合题.cpp](./practical_exercises/key_exercises/read_file.cpp) -├── [异常捕获.cpp](./practical_exercises/key_exercises/异常捕获.cpp) +- [输入输出运算符重载.cpp](./practical_exercises/key_exercises/io_operator_overload.cpp) -├── [类模板之栈.cpp](./practical_exercises/key_exercises/类模板之栈.cpp) +- [输入输出重载.cpp](./practical_exercises/key_exercises/io_operator.cpp) -├── [类模板特化之数组.cpp](./practical_exercises/key_exercises/类模板特化之数组.cpp) +- [输出格式.cpp](./practical_exercises/key_exercises/output.cpp) -├── [继承与封装.cpp](./practical_exercises/key_exercises/继承与封装.cpp) -├── [读写文件综合题.cpp](./practical_exercises/key_exercises/读写文件综合题.cpp) -├── [输入输出运算符重载.cpp](./practical_exercises/key_exercises/输入输出运算符重载.cpp) -├── [输入输出重载.cpp](./practical_exercises/key_exercises/输入输出重载.cpp) -├── [输出格式.cpp](./practical_exercises/key_exercises/输出格式.cpp) +### [C++2.0 新特性](./cpp2.0) -### 3.[C++2.0 新特性](./c++2.0) - -#### 3.0 概况 +#### 概况 C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 -#### 3.1 [C++11 新特性](./c++2.0/c++11) +#### [C++11 新特性](./cpp2.0/cpp11) -- [Variadic Templates](./c++2.0/c++11/variadic) +- [Variadic Templates](./cpp2.0/cpp11/variadic) - Spaces in Template Expressions @@ -172,17 +221,17 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 vector> // before c++ 11 error error: ‘>>’ should be ‘> >’ within a nested template argument list,c++11后可以正常通过 ``` -- [nullptr and nullptr_t](./c++2.0/c++11/nullptr.cpp) +- [nullptr and nullptr_t](./cpp2.0/cpp11/nullptr.cpp) -- [Automatic Type Deduction with auto](./c++2.0/c++11/auto.cpp) +- [Automatic Type Deduction with auto](./cpp2.0/cpp11/auto.cpp) -- [Uniform Initialization ](./c++2.0/c++11/uniform_initialization.cpp) +- [Uniform Initialization ](./cpp2.0/cpp11/uniform_initialization.cpp) -- [initializer_list](./c++2.0/c++11/initializer.cpp) +- [initializer_list](./cpp2.0/cpp11/initializer.cpp) -- [explicit for ctors taking more than one argument](./c++2.0/c++11/explicit.cpp) +- [explicit for ctors taking more than one argument](./cpp2.0/cpp11/explicit.cpp) -- [range-based for statement](./c++2.0/c++11/auto.cpp) +- [range-based for statement](./cpp2.0/cpp11/auto.cpp) ```cpp for(decl:col) { @@ -190,33 +239,33 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 } ``` -- [=default,=delete](./c++2.0/c++11/default_delete.cpp) +- [=default,=delete](./cpp2.0/cpp11/default_delete.cpp) 如果你自行定义了一个 `ctor`, 那么编译器就不会给你一个 `default ctor` 如果强制加上 `=default`, 就可以重新获得并使用 `default ctor`. - Alias(化名) Template (template typedef) - - [alias.cpp](./c++2.0/c++11/alias.cpp) - - [template_template.cpp](./c++2.0/c++11/template_template.cpp) + - [alias.cpp](./cpp2.0/cpp11/alias.cpp) + - [template_template.cpp](./cpp2.0/cpp11/template_template.cpp) -- [template template parameter](./c++2.0/template_template.cpp) +- [template template parameter](./cpp2.0/template_template.cpp) -- [type alias](./c++2.0/c++11/type_alias.cpp) +- [type alias](./cpp2.0/cpp11/type_alias.cpp) -- [noexcept](./c++2.0/c++11/noexcept.cpp) +- [noexcept](./cpp2.0/cpp11/noexcept.cpp) -- [override](./c++2.0/c++11/override.cpp) +- [override](./cpp2.0/cpp11/override.cpp) -- [final](./c++2.0/c++11/final.cpp) +- [final](./cpp2.0/cpp11/final.cpp) -- [decltype](./c++2.0/c++11/decltype.cpp) +- [decltype](./cpp2.0/cpp11/decltype.cpp) -- [lambda](./c++2.0/c++11/lambda.cpp) +- [lambda](./cpp2.0/cpp11/lambda.cpp) -- [Rvalue reference](./c++2.0/c++11/rvalue.cpp) +- [Rvalue reference](./cpp2.0/cpp11/rvalue.cpp) -- [move aware class](./c++2.0/c++11/move.cpp) +- [move aware class](./cpp2.0/cpp11/move.cpp) - 容器-结构与分类 @@ -224,22 +273,22 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 - (2) 关联式容器包括:`set/multiset`,`map/multimap` - (3) 无序容器(C++2.0 新引入,更换原先 `hash_xxx` 为 `unordered_xxx`)包括:`unordered_map/unordered_multimap,unordered_set/unordered_multiset` -- [Hash Function](./c++2.0/c++11/hash.cpp) +- [Hash Function](./cpp2.0/cpp11/hash.cpp) -- [tuple](./c++2.0/c++11/tuple.cpp) +- [tuple](./cpp2.0/cpp11/tuple.cpp) 学习资料:https://www.bilibili.com/video/av51863195?from=search&seid=3610634846288253061 -#### 3.2 C++14/17/20 +#### C++14/17/20 待更新... -### 4.设计模式 +### 设计模式 - [单例模式](./design_pattern/singleton) - [生产消费者模式](./design_pattern/producer_consumer) -### 5. [STL 源码剖析](./src_analysis/stl) +### [STL 源码剖析](./src_analysis/stl) **STL 源码剖析:gcc 4.9.1** @@ -259,18 +308,18 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 - [myhashtable](./src_analysis/stl/myhashtable.md) - [unordered_map](./src_analysis/stl/unordered_map.md) -### 6. [并发编程](./concurrency) +### [并发编程](./concurrency) -#### 6.1 C++ Concurrency in Action +#### C++ Concurrency in Action - [第一章](./concurrency/concurrency_v1/chapter1) - [第二章](./concurrency/concurrency_v1/chapter2) 学习资料:https://downdemo.gitbook.io/cpp-concurrency-in-action-2ed/ -#### 6.2 多线程与多进程 +#### 多线程与多进程 -##### 6.2.1 Threading In C++ +##### Threading In C++ - [介绍](./concurrency/Threading_In_CPlusPlus/1.thread) - [创建线程的五种类型](./concurrency/Threading_In_CPlusPlus/2.create_type) @@ -281,7 +330,7 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 > > https://www.youtube.com/watch?v=eZ8yKZo-PGw&list=PLk6CEY9XxSIAeK-EAh3hB4fgNvYkYmghp&index=4 -### 7. [C++ 惯用法](./codingStyleIdioms) +### [C++ 惯用法](./codingStyleIdioms) ##### 你最喜欢的 c++ 编程风格惯用法是什么? @@ -291,39 +340,39 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 - [4.copy and swap](./codingStyleIdioms/4_copy-swap) - [5.pImpl (指针指向具体实现)](./codingStyleIdioms/5_pImpl) -### 8.学习课程 +### 学习课程 -#### 8.1 [极客时间《现代 C++ 实战 30 讲》](https://time.geekbang.org/channel/home) +#### [极客时间《现代 C++ 实战 30 讲》](https://time.geekbang.org/channel/home) -- [堆、栈、RAII:C++ 里该如何管理资源?](./learn_class/modern_C++_30/RAII) +- [堆、栈、RAII:C++ 里该如何管理资源?](./learn_class/modern_cpp_30/RAII) - [堆](./learn_class/modern_++_30/RAII/heap.cpp) - - [栈](./learn_class/modern_C++_30/RAII/stack.cpp) - - [RAII](./learn_class/modern_C++_30/RAII/RAII.cpp) -- [自己动手,实现 C++ 的智能指针](./learn_class/modern_C++_30/smart_ptr) - - [auto_ptr、scope_ptr](./learn_class/modern_C++_30/smart_ptr/auto_scope.cpp) - - [unique_ptr](./learn_class/modern_C++_30/smart_ptr/unique_ptr.cpp) - - [shared_ptr](./learn_class/modern_C++_30/smart_ptr/shared_ptr.cpp) -- [右值和移动究竟解决了什么问题?](./learn_class/modern_C++_30/reference) - - [左值与右值](./learn_class/modern_C++_30/reference/reference.cpp) - - [延长声明周期](./learn_class/modern_C++_30/reference/lifetime.cpp) - - [引用折叠](./learn_class/modern_C++_30/reference/collapses.cpp) - - [完美转发](./learn_class/modern_C++_30/reference/forward.cpp) - - [不要返回本地变量的引用](./learn_class/modern_C++_30/reference/don'treturnReference.cpp) -- [容器 1](./learn_class/modern_C++_30/container1) -- [容器 2](./learn_class/modern_C++_30/container2) -- [异常](./learn_class/modern_C++_30/exception) -- [字面量、静态断言和成员函数说明符](./learn_class/modern_C++_30/literalAssert) -- [是不是应该返回对象?](./learn_class/modern_C++_30/returnObj) -- [编译期多态:泛型编程和模板入门](./learn_class/modern_C++_30/compilerpoly) -- [译期能做些什么?一个完整的计算世界](./learn_class/modern_C++_30/compilercompute) -- [SFINAE:不是错误的替换失败是怎么回事?](./learn_class/modern_C++_30/SFINAE) -- [constexpr:一个常态的世界](./learn_class/modern_C++_30/constexpr) -- [函数对象和 lambda:进入函数式编程](./learn_class/modern_C++_30/functionLambda) -- [内存模型和 atomic:理解并发的复杂性](./learn_class/modern_C++_30/memorymodel_atomic) - -### 9.工具篇 - -#### 9.1 [容器快捷输出工具](./tool/output) + - [栈](./learn_class/modern_cpp_30/RAII/stack.cpp) + - [RAII](./learn_class/modern_cpp_30/RAII/RAII.cpp) +- [自己动手,实现 C++ 的智能指针](./learn_class/modern_cpp_30/smart_ptr) + - [auto_ptr、scope_ptr](./learn_class/modern_cpp_30/smart_ptr/auto_scope.cpp) + - [unique_ptr](./learn_class/modern_cpp_30/smart_ptr/unique_ptr.cpp) + - [shared_ptr](./learn_class/modern_cpp_30/smart_ptr/shared_ptr.cpp) +- [右值和移动究竟解决了什么问题?](./learn_class/modern_cpp_30/reference) + - [左值与右值](./learn_class/modern_cpp_30/reference/reference.cpp) + - [延长声明周期](./learn_class/modern_cpp_30/reference/lifetime.cpp) + - [引用折叠](./learn_class/modern_cpp_30/reference/collapses.cpp) + - [完美转发](./learn_class/modern_cpp_30/reference/forward.cpp) + - [不要返回本地变量的引用](./learn_class/modern_cpp_30/reference/don'treturnReference.cpp) +- [容器 1](./learn_class/modern_cpp_30/container1) +- [容器 2](./learn_class/modern_cpp_30/container2) +- [异常](./learn_class/modern_cpp_30/exception) +- [字面量、静态断言和成员函数说明符](./learn_class/modern_cpp_30/literalAssert) +- [是不是应该返回对象?](./learn_class/modern_cpp_30/returnObj) +- [编译期多态:泛型编程和模板入门](./learn_class/modern_cpp_30/compilerpoly) +- [译期能做些什么?一个完整的计算世界](./learn_class/modern_cpp_30/compilercompute) +- [SFINAE:不是错误的替换失败是怎么回事?](./learn_class/modern_cpp_30/SFINAE) +- [constexpr:一个常态的世界](./learn_class/modern_cpp_30/constexpr) +- [函数对象和 lambda:进入函数式编程](./learn_class/modern_cpp_30/functionLambda) +- [内存模型和 atomic:理解并发的复杂性](./learn_class/modern_cpp_30/memorymodel_atomic) + +### 工具篇 + +#### [容器快捷输出工具](./tool/output) 对吴老师的代码进行了修改,[点击此处直通代码](./tool/output/output_container.h) @@ -343,28 +392,28 @@ map mp{ { 1 => 1, 2 => 4, 3 => 9 } ``` -#### 9.2 像 Python 一样简单输出(Jupyter Notebook) +#### 像 Python 一样简单输出(Jupyter Notebook) - [像 Python 一样玩 C/C++](./tool/像Python一样玩CC++.md) -#### 9.3 观察编译过程变化 +#### 观察编译过程变化 - [https://cppinsights.io](https://cppinsights.io/) -#### 9.4 C++ 的 Debug 工具 dbg-macro +#### C++ 的 Debug 工具 dbg-macro - [C++ 的 Debug 工具 dbg-macro](./tool/C++的Debug工具dbg-macro.md) -#### 9.5 在 Linux 上的 Debug 工具 rr - 拥有回到过去的能力 +#### 在 Linux 上的 Debug 工具 rr - 拥有回到过去的能力 - [用 rr 来进行 debug](./tool/用rr来进行debug.md) -### 10.拓展部分 +### 拓展部分 -#### 10.1 一些问题 +#### 一些问题 - [C++ 中如何将 string 类型转换为 int 类型?](./extension/some_problem/string_int.md) -### 11.贡献者 +### 贡献者 @@ -376,22 +425,5 @@ map mp{ -### 12.赞助我 - -如果觉得不错,赞助我吧~ - - - - - - - -
-
- 支付宝 -
-
- 微信 -
diff --git a/README_EN.md b/README_EN.md index a43f6ed7..77f6bc94 100644 --- a/README_EN.md +++ b/README_EN.md @@ -1,7 +1,7 @@ # Stories About C Plus Plus Thanks for all the support about << Stories About C ++ >>~~, right now you could open this link(https://light-city.club/sc/) to read this article.~~ -Any star, retweet or pr will be weclomed. +Any star, retweet or pr will be welcomed. **Notice: The website is not available now.** @@ -54,7 +54,7 @@ other details,such as in-depth source code analysis and multi-threaded concurren Basic Grammar About C ++ - [x] day2 -Recursive、Structor、Enumerate、Static Variable +Recursive、Struct、Enumerate、Static Variable - [x] day3 @@ -70,7 +70,7 @@ Inheritance and Polymorphism - [x] day6 -Vitrual Function、Abstruct +Virtual Function、Abstruct - [x] day7 @@ -232,7 +232,7 @@ Learning materials: https://chenxiaowei.gitbook.io/cpp_concurrency_in_action/ - [Introduction](./concurrency/Threading_In_CPlusPlus/1.thread) - [Five Types of Thread Creation](./concurrency/Threading_In_CPlusPlus/2.create_type) -- [Join And Detachs](./concurrency/Threading_In_CPlusPlus/3.join_detach) +- [Join And Detaches](./concurrency/Threading_In_CPlusPlus/3.join_detach) - [mutex in C++ Threading](./concurrency/Threading_In_CPlusPlus/4.mutex) > From: @@ -253,31 +253,31 @@ Learning materials: https://chenxiaowei.gitbook.io/cpp_concurrency_in_action/ #### 6.1 [Chinese Name:极客时间《现代 C++ 实战 30 讲》](https://time.geekbang.org/channel/home) -- [heap、stack、RAII:How to manage resources for C ++ ?](./learn_class/modern_C++_30/RAII) +- [heap、stack、RAII:How to manage resources for C ++ ?](./learn_class/modern_cpp_30/RAII) - [heap](./modern_++_30/RAII/heap.cpp) - - [stack](./learn_class/modern_C++_30/RAII/stack.cpp) - - [RAII](./learn_class/modern_C++_30/RAII/RAII.cpp) -- [Implementing smart pointers for C ++](./learn_class/modern_C++_30/smart_ptr) - - [auto_ptr、scope_ptr](./learn_class/modern_C++_30/smart_ptr/auto_scope.cpp) - - [unique_ptr](./learn_class/modern_C++_30/smart_ptr/unique_ptr.cpp) - - [shared_ptr](./learn_class/modern_C++_30/smart_ptr/shared_ptr.cpp) -- [What exactly does r value and move solve?](./learn_class/modern_C++_30/reference) - - [L value and R value](./learn_class/modern_C++_30/reference/reference.cpp) - - [Extend the declaration cycle](./learn_class/modern_C++_30/reference/lifetime.cpp) - - [Reference folding](./learn_class/modern_C++_30/reference/collapses.cpp) - - [Perfect forward](./learn_class/modern_C++_30/reference/forward.cpp) - - [Do not return Reference](./learn_class/modern_C++_30/reference/don'treturnReference.cpp) -- [Container 1](./learn_class/modern_C++_30/container1) -- [Container 2](./learn_class/modern_C++_30/container2) -- [Exception](./learn_class/modern_C++_30/exception) -- [Literal、Static Assertion And Member Function Specifier](./learn_class/modern_C++_30/literalAssert) -- [Return Object?](./learn_class/modern_C++_30/returnObj)c -- [Getting started with generic programming and templates](./learn_class/modern_C++_30/compilerpoly) -- [A whole Compiler Compute World](./learn_class/modern_C++_30/compilercompute) -- [SFINAE:What is it if it is not replace error?](./learn_class/modern_C++_30/SFINAE) -- [constexpr:A Normal World](./learn_class/modern_C++_30/constexpr) -- [Function object and Lambda:functionLambda](./learn_class/modern_C++_30/functionLambda) -- [Memory Model and Atomic:Understangding the complexity of concurrency](./learn_class/modern_C++_30/memorymodel_atomic) + - [stack](./learn_class/modern_cpp_30/RAII/stack.cpp) + - [RAII](./learn_class/modern_cpp_30/RAII/RAII.cpp) +- [Implementing smart pointers for C ++](./learn_class/modern_cpp_30/smart_ptr) + - [auto_ptr、scope_ptr](./learn_class/modern_cpp_30/smart_ptr/auto_scope.cpp) + - [unique_ptr](./learn_class/modern_cpp_30/smart_ptr/unique_ptr.cpp) + - [shared_ptr](./learn_class/modern_cpp_30/smart_ptr/shared_ptr.cpp) +- [What exactly does r value and move solve?](./learn_class/modern_cpp_30/reference) + - [L value and R value](./learn_class/modern_cpp_30/reference/reference.cpp) + - [Extend the declaration cycle](./learn_class/modern_cpp_30/reference/lifetime.cpp) + - [Reference folding](./learn_class/modern_cpp_30/reference/collapses.cpp) + - [Perfect forward](./learn_class/modern_cpp_30/reference/forward.cpp) + - [Do not return Reference](./learn_class/modern_cpp_30/reference/don'treturnReference.cpp) +- [Container 1](./learn_class/modern_cpp_30/container1) +- [Container 2](./learn_class/modern_cpp_30/container2) +- [Exception](./learn_class/modern_cpp_30/exception) +- [Literal、Static Assertion And Member Function Specifier](./learn_class/modern_cpp_30/literalAssert) +- [Return Object?](./learn_class/modern_cpp_30/returnObj)c +- [Getting started with generic programming and templates](./learn_class/modern_cpp_30/compilerpoly) +- [A whole Compiler Compute World](./learn_class/modern_cpp_30/compilercompute) +- [SFINAE:What is it if it is not replace error?](./learn_class/modern_cpp_30/SFINAE) +- [constexpr:A Normal World](./learn_class/modern_cpp_30/constexpr) +- [Function object and Lambda:functionLambda](./learn_class/modern_cpp_30/functionLambda) +- [Memory Model and Atomic:Understanding the complexity of concurrency](./learn_class/modern_cpp_30/memorymodel_atomic) ### 7.Tools diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/basic_content/abstract/BUILD b/basic_content/abstract/BUILD new file mode 100644 index 00000000..11411032 --- /dev/null +++ b/basic_content/abstract/BUILD @@ -0,0 +1,43 @@ +# you can run some main program, just replace binary name +# such as: `bazel run basic_content/abstract:interesting_facts1` +# `bazel run basic_content/abstract:interesting_facts2` +# etc... +load("@rules_cc//cc:defs.bzl", "cc_binary") + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "interesting_facts1", + srcs = ["interesting_facts1.cpp"], +) + +cc_binary( + name = "interesting_facts2", + srcs = ["interesting_facts2.cpp"], +) + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "interesting_facts3", + srcs = ["interesting_facts3.cpp"], +) + +cc_binary( + name = "interesting_facts4", + srcs = ["interesting_facts4.cpp"], +) + +cc_binary( + name = "interesting_facts5", + srcs = ["interesting_facts5.cpp"], +) + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "pure_virtual", + srcs = ["pure_virtual.cpp"], +) + +cc_binary( + name = "derived_full", + srcs = ["derived_full.cpp"], +) \ No newline at end of file diff --git a/basic_content/abstract/README.md b/basic_content/abstract/README.md index e11aa358..15f3ae64 100644 --- a/basic_content/abstract/README.md +++ b/basic_content/abstract/README.md @@ -23,7 +23,7 @@ public: 抽象类只能作为基类来派生新类使用,不能创建抽象类的对象,抽象类的指针和引用->由抽象类派生出来的类的对象! -> 代码样例:[test.cpp](./test.cpp)、[pure_virtual.cpp](./pure_virtual.cpp) +> 代码样例:[abstract_base.h](./abstract_base.h)、[pure_virtual.cpp](./pure_virtual.cpp) ## 2.实现抽象类 diff --git a/basic_content/abstract/abstract.cpp b/basic_content/abstract/abstract.cpp index 3a26d992..a7db6188 100644 --- a/basic_content/abstract/abstract.cpp +++ b/basic_content/abstract/abstract.cpp @@ -1,27 +1,28 @@ /** * @file abstract.cpp - * @brief 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数 + * @brief + * 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数 * 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类 * @author 光城 * @version v1 * @date 2019-07-20 */ -#include +#include using namespace std; class A { public: - virtual void f() = 0; // 纯虚函数 - void g(){ this->f(); } - A(){} + virtual void f() = 0; // 纯虚函数 + void g() { this->f(); } + A() {} }; -class B:public A{ +class B : public A { public: - void f(){ cout<<"B:f()"< -using namespace std; +#include +using namespace std; -class Base -{ - int x; - public: - virtual void fun() = 0; - int getX() { return x; } -}; +class Base { + int x; -class Derived: public Base -{ - int y; - public: - void fun() { cout << "fun() called"; } // 实现了fun()函数 -}; +public: + virtual void fun() = 0; + int getX() { return x; } +}; -int main(void) -{ - Derived d; - d.fun(); - return 0; -} +class Derived : public Base { +public: + void fun() { cout << "fun() called"; } // 实现了fun()函数 +}; + +int main(void) { + Derived d; + d.fun(); + return 0; +} diff --git a/basic_content/abstract/interesting_facts1.cpp b/basic_content/abstract/interesting_facts1.cpp index 048ede79..e36a9312 100644 --- a/basic_content/abstract/interesting_facts1.cpp +++ b/basic_content/abstract/interesting_facts1.cpp @@ -6,23 +6,21 @@ * @date 2019-07-20 */ -#include -using namespace std; +#include +using namespace std; /** * @brief 抽象类至少包含一个纯虚函数 */ -class Test -{ - int x; -public: - virtual void show() = 0; - int getX() { return x; } -}; +class Test { + int x; -int main(void) -{ - Test t; //error! 不能创建抽象类的对象 - return 0; -} +public: + virtual void show() = 0; + int getX() { return x; } +}; +int main(void) { + Test t; // error! 不能创建抽象类的对象 + return 0; +} diff --git a/basic_content/abstract/interesting_facts2.cpp b/basic_content/abstract/interesting_facts2.cpp index 84d293e7..c9e002fd 100644 --- a/basic_content/abstract/interesting_facts2.cpp +++ b/basic_content/abstract/interesting_facts2.cpp @@ -6,33 +6,28 @@ * @date 2019-07-20 */ -#include -using namespace std; - +#include +using namespace std; /** * @brief 抽象类至少包含一个纯虚函数 */ -class Base -{ - int x; -public: - virtual void show() = 0; - int getX() { return x; } - -}; -class Derived: public Base -{ -public: - void show() { cout << "In Derived \n"; } - Derived(){} -}; -int main(void) -{ - //Base b; //error! 不能创建抽象类的对象 - //Base *b = new Base(); error! - Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象 - bp->show(); - return 0; -} +class Base { + int x; +public: + virtual void show() = 0; + int getX() { return x; } +}; +class Derived : public Base { +public: + void show() { cout << "In Derived \n"; } + Derived() {} +}; +int main(void) { + // Base b; //error! 不能创建抽象类的对象 + // Base *b = new Base(); error! + Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象 + bp->show(); + return 0; +} diff --git a/basic_content/abstract/interesting_facts3.cpp b/basic_content/abstract/interesting_facts3.cpp index fb768242..dcb56f25 100644 --- a/basic_content/abstract/interesting_facts3.cpp +++ b/basic_content/abstract/interesting_facts3.cpp @@ -6,24 +6,23 @@ * @date 2019-07-20 */ -#include -using namespace std; +#include +using namespace std; -class Base -{ - int x; -public: - virtual void show() = 0; - int getX() { return x; } -}; -class Derived: public Base -{ -public: -// void show() { } -}; -int main(void) -{ - Derived d; //error! 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象 - return 0; -} +class Base { + int x; +public: + virtual void show() = 0; + int getX() { return x; } +}; +class Derived : public Base { +public: + // void show() { } +}; +int main(void) { + Derived + d; // error! + // 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象 + return 0; +} diff --git a/basic_content/abstract/interesting_facts4.cpp b/basic_content/abstract/interesting_facts4.cpp index 028554be..155e4233 100644 --- a/basic_content/abstract/interesting_facts4.cpp +++ b/basic_content/abstract/interesting_facts4.cpp @@ -6,30 +6,29 @@ * @date 2019-07-20 */ -#include -using namespace std; +#include +using namespace std; -// An abstract class with constructor -class Base -{ - protected: - int x; - public: - virtual void fun() = 0; - Base(int i) { x = i; } -}; +// An abstract class with constructor +class Base { +protected: + int x; -class Derived: public Base -{ - int y; - public: - Derived(int i, int j):Base(i) { y = j; } - void fun() { cout << "x = " << x << ", y = " << y; } -}; +public: + virtual void fun() = 0; + Base(int i) { x = i; } +}; -int main(void) -{ - Derived d(4, 5); - d.fun(); - return 0; -} +class Derived : public Base { + int y; + +public: + Derived(int i, int j) : Base(i) { y = j; } + void fun() { cout << "x = " << x << ", y = " << y; } +}; + +int main(void) { + Derived d(4, 5); + d.fun(); + return 0; +} diff --git a/basic_content/abstract/interesting_facts5.cpp b/basic_content/abstract/interesting_facts5.cpp index e9152b90..20669985 100644 --- a/basic_content/abstract/interesting_facts5.cpp +++ b/basic_content/abstract/interesting_facts5.cpp @@ -7,24 +7,23 @@ * @version v1 * @date 2019-07-20 */ -#include +#include using namespace std; -class Base { - public: - Base() { cout << "Constructor: Base" << endl; } - virtual ~Base() { cout << "Destructor : Base" << endl; } +class Base { +public: + Base() { cout << "Constructor: Base" << endl; } + virtual ~Base() { cout << "Destructor : Base" << endl; } }; -class Derived: public Base { - public: - Derived() { cout << "Constructor: Derived" << endl; } - ~Derived() { cout << "Destructor : Derived" << endl; } +class Derived : public Base { +public: + Derived() { cout << "Constructor: Derived" << endl; } + ~Derived() { cout << "Destructor : Derived" << endl; } }; -int main() { - Base *Var = new Derived(); - delete Var; - return 0; +int main() { + Base *Var = new Derived(); + delete Var; + return 0; } - diff --git a/basic_content/abstract/pure_virtual.cpp b/basic_content/abstract/pure_virtual.cpp index bfaf1777..2bba32f3 100644 --- a/basic_content/abstract/pure_virtual.cpp +++ b/basic_content/abstract/pure_virtual.cpp @@ -2,33 +2,31 @@ * @file pure_virtual.cpp * @brief 纯虚函数:没有函数体的虚函数 * 抽象类:包含纯虚函数的类 - * + * * @author 光城 * @version v1 * @date 2019-07-20 */ -#include +#include using namespace std; -class A -{ +class A { private: - int a; + int a; + public: - virtual void show()=0; // 纯虚函数 + virtual void show() = 0; // 纯虚函数 }; +int main() { + /* + * 1. 抽象类只能作为基类来派生新类使用 + * 2. 抽象类的指针和引用->由抽象类派生出来的类的对象! + */ + A a; // error 抽象类,不能创建对象 -int main() -{ - /* - * 1. 抽象类只能作为基类来派生新类使用 - * 2. 抽象类的指针和引用->由抽象类派生出来的类的对象! - */ - A a; // error 抽象类,不能创建对象 - - A *a1; // ok 可以定义抽象类的指针 + A *a1; // ok 可以定义抽象类的指针 - A *a2 = new A(); // error, A是抽象类,不能创建对象 + A *a2 = new A(); // error, A是抽象类,不能创建对象 } diff --git a/basic_content/abstract/test.cpp b/basic_content/abstract/test.cpp deleted file mode 100644 index 7797c909..00000000 --- a/basic_content/abstract/test.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test.cpp - * @brief C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数! - * 纯虚函数:没有函数体的虚函数 - * @author 光城 - * @version v1 - * @date 2019-07-20 - */ - - - -/** - * @brief 抽象类 - */ -class Test -{ - // Data members of class -public: - // Pure Virtual Function - virtual void show() = 0; - - /* Other members */ -}; diff --git a/basic_content/assert/BUILD b/basic_content/assert/BUILD new file mode 100644 index 00000000..d0279517 --- /dev/null +++ b/basic_content/assert/BUILD @@ -0,0 +1,13 @@ +# you can run some main program, just replace binary name +# such as: `bazel run basic_content/assert:ignore_assert` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "ignore_assert", + srcs = ["ignore_assert.c"], +) + +cc_binary( + name = "assert", + srcs = ["assert.c"], +) diff --git a/basic_content/assert/assert.c b/basic_content/assert/assert.c index f66a2c04..71b24adb 100644 --- a/basic_content/assert/assert.c +++ b/basic_content/assert/assert.c @@ -6,7 +6,8 @@ int main() int x = 7; /* Some big code in between and let's say x - * is accidentally changed to 9 */ + * is accidentally changed to 9 + */ x = 9; // Programmer assumes x to be 7 in rest of the code diff --git a/basic_content/assert/ignore_assert.c b/basic_content/assert/ignore_assert.c index 0a5a15e1..d7f941e4 100644 --- a/basic_content/assert/ignore_assert.c +++ b/basic_content/assert/ignore_assert.c @@ -11,7 +11,6 @@ #include int main(){ - int x=7; assert(x==5); return 0; } diff --git a/basic_content/bit/BUILD b/basic_content/bit/BUILD new file mode 100644 index 00000000..cbd6d03d --- /dev/null +++ b/basic_content/bit/BUILD @@ -0,0 +1,7 @@ +# please run `bazel run basic_content/bit:bit` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "bit", + srcs = ["bit.cpp"], +) \ No newline at end of file diff --git a/basic_content/bit/README.md b/basic_content/bit/README.md index 69fd9ffe..c5b40d6a 100644 --- a/basic_content/bit/README.md +++ b/basic_content/bit/README.md @@ -108,7 +108,7 @@ struct stuff 这里 stuff.field1 与 stuff.field2 之间有一个 2 Bits 的空隙,stuff.field3 则存储在下一个 unsigned int 中,该结构现在大小为 3 * 32 = 96 Bits。 -学习代码见:[learn.cpp](learn.cpp) +学习代码见:[bit.cpp](bit.cpp) ## 位域的初始化和位的重映射 diff --git a/basic_content/bit/bit.cpp b/basic_content/bit/bit.cpp new file mode 100644 index 00000000..70cf7d3a --- /dev/null +++ b/basic_content/bit/bit.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; +struct stuff { + unsigned int field1 : 30; + unsigned int : 2; + unsigned int field2 : 4; + unsigned int : 0; + unsigned int field3 : 3; +}; +int main() { + struct stuff s = {1, 3, 5}; + cout << s.field1 << endl; + cout << s.field2 << endl; + cout << s.field3 << endl; + cout << sizeof(s) << endl; + return 0; +} diff --git a/basic_content/bit/learn.cpp b/basic_content/bit/learn.cpp deleted file mode 100644 index 505256bc..00000000 --- a/basic_content/bit/learn.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include - -using namespace std; -struct stuff -{ - unsigned int field1: 30; - unsigned int : 2; - unsigned int field2: 4; - unsigned int : 0; - unsigned int field3: 3; -}; -int main(){ - struct stuff s={1,3,5}; - cout< using namespace std; -class A -{ - public: - virtual void f()//虚函数实现 - { - cout << "Base A::f() " << endl; - } +class A { +public: + virtual void f() //虚函数实现 + { + cout << "Base A::f() " << endl; + } }; -class B:public A // 必须为共有继承,否则后面调用不到,class默认为私有继承! +class B : public A // 必须为共有继承,否则后面调用不到,class默认为私有继承! { - public: - virtual void f()//虚函数实现,子类中virtual关键字可以没有 - { - cout << "Derived B::f() " << endl; - } +public: + virtual void f() //虚函数实现,子类中virtual关键字可以没有 + { + cout << "Derived B::f() " << endl; + } }; +int main() { + A a; //基类对象 + B b; //派生类对象 -int main() -{ - A a;//基类对象 - B b;//派生类对象 - - A* pa = &a;//父类指针指向父类对象 - pa->f();//调用父类的函数 + A *pa = &a; //父类指针指向父类对象 + pa->f(); //调用父类的函数 - pa = &b; //父类指针指向子类对象,多态实现 - pa->f();//调用派生类同名函数 - return 0; + pa = &b; //父类指针指向子类对象,多态实现 + pa->f(); //调用派生类同名函数 + return 0; } diff --git a/basic_content/c_poly/c_examp b/basic_content/c_poly/c_examp deleted file mode 100755 index 458fb2a5..00000000 Binary files a/basic_content/c_poly/c_examp and /dev/null differ diff --git a/basic_content/const/BUILD b/basic_content/const/BUILD new file mode 100644 index 00000000..3c294fce --- /dev/null +++ b/basic_content/const/BUILD @@ -0,0 +1,17 @@ +# please run `bazel run basic_content/const:const_function` +# please run `bazel run basic_content/const:const_num` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "const_function", + srcs = ["const_function.cpp"], + copts = ["-std=c++11"] +) + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "const_num", + srcs = ["const_num.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/const/README.md b/basic_content/const/README.md index 24153a79..cf1367dc 100644 --- a/basic_content/const/README.md +++ b/basic_content/const/README.md @@ -19,7 +19,7 @@ const int a=100; + 类型检查 + const常量与`#define`宏定义常量的区别: - > ~~**const常量具有类型,编译器可以进行安全检查;#define宏定义没有数据类型,只是简单的字符串替换,不能进行安全检查。**~~感谢两位大佬指出这里问题,见:[issue](https://github.com/Light-City/CPlusPlusThings/issues/5) + > const常量具有类型,编译器可以进行安全检查;#define宏定义没有数据类型,只是简单的字符串替换,不能进行安全检查。感谢两位大佬指出这里问题,见:[issue](https://github.com/Light-City/CPlusPlusThings/issues/5) + const常量支持所有类型。 + 其他情况下它只是一个 `const` 限定的变量,不要将与常量混淆。 @@ -28,7 +28,7 @@ const int a=100; ```cpp void f(const int i){ - i++; //error! + i++; // error! } ``` @@ -58,9 +58,9 @@ int main(){ > const常量在不同文件的访问 ```cpp -//extern_file1.cpp +// extern_file1.cpp extern const int ext=12; -//extern_file2.cpp +// extern_file2.cpp #include extern const int ext; int main(){ @@ -88,22 +88,34 @@ const int i,j=0 // error: uninitialized const ‘i’ 与指针相关的const有四种: ```c++ -const char * a; //指向const对象的指针或者说指向常量的指针。 -char const * a; //同上 -char * const a; //指向类型对象的const指针。或者说常指针、const指针。 -const char * const a; //指向const对象的const指针。 +const char * a; // 指向const对象的指针或者说指向常量的指针。 +char const * a; // 同上 +char * const a; // 指向类型对象的const指针。或者说常指针、const指针。 +const char * const a; // 指向const对象的const指针。 ``` > **小结:**
> 如果*const*位于`*`的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;
如果const位于`*`的右侧,*const*就是修饰指针本身,即指针本身是常量。 +**另一种解读方式**
+利用英文从右边往左边读,并且以to为分界,to之前为描述指针的特性,to之后为描述目标的特性
+```c++ +const char * p; //p is a pointer to const char +char const * p; //同上 +char * const p; //p is a const pointer to char +const char * const p; //p is a const pointer to const char +``` +当指针被加上const特性,则指针不可改变指向的地址
+当指向的目标特性为char,则内容可以透过指针被修改,如: *char='y';
+当指向的目标特性为const char,则内容不可透过指针修改
+ 具体使用如下: (1) **指向常量的指针** ```cpp const int *ptr; -*ptr = 10; //error +*ptr = 10; // error ``` ptr是一个指向int类型const对象的指针,const定义的是int类型,也就是ptr所指向的对象类型,而不是ptr本身,所以ptr可以不用赋初始值。但是不能通过ptr去修改所指对象的值。 @@ -113,7 +125,7 @@ ptr是一个指向int类型const对象的指针,const定义的是int类型, ```c++ const int p = 10; const void * vp = &p; -void *vp = &p; //error +void *vp = &p; // error ``` 另外一个重点是:**允许把非const对象的地址赋给指向const对象的指针**。 @@ -123,7 +135,7 @@ void *vp = &p; //error ```c++ const int *ptr; int val = 3; -ptr = &val; //ok +ptr = &val; // ok ``` 我们不能通过ptr指针来修改val的值,即使它指向的是非const对象! @@ -140,22 +152,33 @@ cout<<*ptr< using namespace std; int main(){ + int num=0, num1=1; + int * const ptr=# // const指针必须初始化!且const指针的指向不能修改 + ptr = &num1; // error! const指针不能修改指向! + cout<<*ptr< +using namespace std; +int main(){ + int num=0, num1=1; + int * const ptr=# // const指针必须初始化!且const指针的指向不能修改 + *ptr = 1; cout<<*ptr< int `*`错误! @@ -164,7 +187,7 @@ int main(){ using namespace std; int main(){ const int num=0; - int * const ptr=# //error! const int* -> int* + int * const ptr=# // error! const int* -> int* cout<<*ptr< -#include"apple.cpp" -using namespace std; - -Apple::Apple(int i):apple_number(i) +// apple.cpp +Apple::Apple(int i) : apple_number(i) { - } -int Apple::add(){ - take(1); +int Apple::add(int num) +{ + take(num); return 0; } -int Apple::add(int num) const{ +int Apple::add(int num) const +{ take(num); - return num; + return 0; } void Apple::take(int num) const { - cout<<"take func "< 编译: g++ -o main main.cpp apple.cpp
+> 编译:bazel run basic_content/const/class_const/first_example:main
此时报错,上面getCount()方法中调用了一个add方法,而add方法并非const修饰,所以运行报错。也就是说const成员函数只能访问const成员函数。 diff --git a/basic_content/const/class_const/c++11_example/BUILD b/basic_content/const/class_const/c++11_example/BUILD new file mode 100644 index 00000000..e5096a64 --- /dev/null +++ b/basic_content/const/class_const/c++11_example/BUILD @@ -0,0 +1,16 @@ +# please run `bazel run basic_content/const/class_const/c++11_example:main` +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") + +cc_library( + name = "apple", + srcs = ["apple.cpp"], + hdrs = ["apple.h"], +) + +cc_binary( + name = "main", + srcs = ["main.cpp"], + deps = [ + ":apple", + ], +) \ No newline at end of file diff --git a/basic_content/const/class_const/c++11_example/apple.cpp b/basic_content/const/class_const/c++11_example/apple.cpp index e4988644..2f39fd6b 100644 --- a/basic_content/const/class_const/c++11_example/apple.cpp +++ b/basic_content/const/class_const/c++11_example/apple.cpp @@ -1,15 +1,19 @@ -class Apple -{ -private: - int people[100]; -public: - Apple(int i); - //使用c++11标准编译 - static const int apple_number=10; - //const int apple_number=10; - void take(int num) const; - int add(int num); - int add(int num) const; - int getCount() const; -}; +#include "apple.h" +#include +using namespace std; +Apple::Apple(int i) {} +int Apple::add(int num) { + take(num); + return 0; +} +int Apple::add(int num) const { + take(num); + return 0; +} +void Apple::take(int num) const { cout << "take func " << num << endl; } +int Apple::getCount() const { + take(1); + // add(); //error + return apple_number; +} \ No newline at end of file diff --git a/basic_content/const/class_const/c++11_example/apple.h b/basic_content/const/class_const/c++11_example/apple.h new file mode 100644 index 00000000..edd65ff1 --- /dev/null +++ b/basic_content/const/class_const/c++11_example/apple.h @@ -0,0 +1,13 @@ +#pragma once + +class Apple { +public: + Apple(int i); + // 使用c++11标准编译 + static const int apple_number = 10; + // const int apple_number=10; + void take(int num) const; + int add(int num); + int add(int num) const; + int getCount() const; +}; diff --git a/basic_content/const/class_const/c++11_example/main b/basic_content/const/class_const/c++11_example/main deleted file mode 100755 index 62017da6..00000000 Binary files a/basic_content/const/class_const/c++11_example/main and /dev/null differ diff --git a/basic_content/const/class_const/c++11_example/main.cpp b/basic_content/const/class_const/c++11_example/main.cpp index a72adb85..95c91e6e 100644 --- a/basic_content/const/class_const/c++11_example/main.cpp +++ b/basic_content/const/class_const/c++11_example/main.cpp @@ -1,31 +1,11 @@ -#include -#include"apple.cpp" +#include "apple.h" +#include using namespace std; -Apple::Apple(int i) -{ - -} -int Apple::add(int num){ - take(num); -} -int Apple::add(int num) const{ - take(num); -} -void Apple::take(int num) const -{ - cout<<"take func "< +Apple::Apple(int i) : apple_number(i) {} +int Apple::add(int num) { + take(num); + return 0; +} +int Apple::add(int num) const { + take(num); + return 0; +} +void Apple::take(int num) const { + std::cout << "take func " << num << std::endl; +} +int Apple::getCount() const { + take(1); + // add(); //error + return apple_number; +} \ No newline at end of file diff --git a/basic_content/const/class_const/first_example/apple.h b/basic_content/const/class_const/first_example/apple.h new file mode 100644 index 00000000..7222b025 --- /dev/null +++ b/basic_content/const/class_const/first_example/apple.h @@ -0,0 +1,11 @@ +#pragma once + +class Apple { +public: + Apple(int i); + const int apple_number; + void take(int num) const; + int add(int num); + int add(int num) const; + int getCount() const; +}; diff --git a/basic_content/const/class_const/first_example/main.cpp b/basic_content/const/class_const/first_example/main.cpp index 40056f2e..83742e73 100644 --- a/basic_content/const/class_const/first_example/main.cpp +++ b/basic_content/const/class_const/first_example/main.cpp @@ -1,32 +1,12 @@ -#include -#include"apple.cpp" +#include "apple.h" +#include using namespace std; -Apple::Apple(int i):apple_number(i) -{ - -} -int Apple::add(int num){ - take(num); -} -int Apple::add(int num) const{ - take(num); -} -void Apple::take(int num) const -{ - cout<<"take func "< +int Apple::apple_number = 10; + +Apple::Apple(int i) { apple_number = i; } +int Apple::add() { + take(1); + return 0; +} +int Apple::add(int num) const { + take(num); + return num; +} +void Apple::take(int num) const { + std::cout << "take func " << num << std::endl; +} +int Apple::getCount() const { + take(1); + add(); // error + return apple_number; +} \ No newline at end of file diff --git a/basic_content/const/class_const/overload_example/apple.h b/basic_content/const/class_const/overload_example/apple.h new file mode 100644 index 00000000..6957e810 --- /dev/null +++ b/basic_content/const/class_const/overload_example/apple.h @@ -0,0 +1,11 @@ +#pragma once + +class Apple { +public: + Apple(int i); + static int apple_number; + void take(int num) const; + int add(); + int add(int num) const; + int getCount() const; +}; diff --git a/basic_content/const/class_const/overload_example/main b/basic_content/const/class_const/overload_example/main deleted file mode 100755 index 562af2ed..00000000 Binary files a/basic_content/const/class_const/overload_example/main and /dev/null differ diff --git a/basic_content/const/class_const/overload_example/main.cpp b/basic_content/const/class_const/overload_example/main.cpp index 4ef0868c..48980125 100644 --- a/basic_content/const/class_const/overload_example/main.cpp +++ b/basic_content/const/class_const/overload_example/main.cpp @@ -1,38 +1,12 @@ -#include -#include"apple.cpp" +#include "apple.h" +#include using namespace std; -const int Apple::apple_number=10; -#include -#include"apple.cpp" -using namespace std; - -Apple::Apple(int i):apple_number(i) -{ -} -int Apple::add(){ - take(1); - return 0; -} -int Apple::add(int num) const{ - take(num); - return num; -} -void Apple::take(int num) const -{ - cout<<"take func "< +const int Apple::apple_number = 10; +int Apple::ap = 666; +Apple::Apple(int i) {} +int Apple::add(int num) { + take(num); + return 0; +} +int Apple::add(int num) const { + take(num); + return 0; +} +void Apple::take(int num) const { + std::cout << "take func " << num << std::endl; +} +int Apple::getCount() const { + take(1); + // add(); //error + return apple_number; +} \ No newline at end of file diff --git a/basic_content/const/class_const/static_example/apple.h b/basic_content/const/class_const/static_example/apple.h new file mode 100644 index 00000000..7050d0ac --- /dev/null +++ b/basic_content/const/class_const/static_example/apple.h @@ -0,0 +1,11 @@ +#pragma once +class Apple { +public: + Apple(int i); + static int ap; // 在类实现文件中定义并初始化 + static const int apple_number; + void take(int num) const; + int add(int num); + int add(int num) const; + int getCount() const; +}; diff --git a/basic_content/const/class_const/static_example/main b/basic_content/const/class_const/static_example/main deleted file mode 100755 index 3c839113..00000000 Binary files a/basic_content/const/class_const/static_example/main and /dev/null differ diff --git a/basic_content/const/class_const/static_example/main.cpp b/basic_content/const/class_const/static_example/main.cpp index 74ca4f0e..c2ecd1d4 100644 --- a/basic_content/const/class_const/static_example/main.cpp +++ b/basic_content/const/class_const/static_example/main.cpp @@ -1,34 +1,11 @@ -#include -#include"apple.cpp" -using namespace std; -const int Apple::apple_number=10; -int Apple::ap=666; -Apple::Apple(int i) -{ - -} -int Apple::add(int num){ - take(num); -} -int Apple::add(int num) const{ - take(num); -} -void Apple::take(int num) const -{ - cout<<"take func "< +int main() { + Apple a(2); + std::cout << a.getCount() << std::endl; + std::cout << a.ap << std::endl; + a.add(10); + const Apple b(3); + b.add(100); + return 0; } diff --git a/basic_content/const/const_function.cpp b/basic_content/const/const_function.cpp index d45c3bf3..66403a86 100644 --- a/basic_content/const/const_function.cpp +++ b/basic_content/const/const_function.cpp @@ -1,11 +1,9 @@ -#include +#include using namespace std; -void f(const int i){ - i=10; // error: assignment of read-only parameter ‘i’ - cout< +#include using namespace std; -int main(){ - const int b = 10; - b = 0; //error - const string s = "helloworld"; - const int i,j=0; +int main() { + const int b = 10; + b = 0; // error + const string s = "helloworld"; + const int i, j = 0; } diff --git a/basic_content/const/extern_const/BUILD b/basic_content/const/extern_const/BUILD new file mode 100644 index 00000000..ce887e83 --- /dev/null +++ b/basic_content/const/extern_const/BUILD @@ -0,0 +1,14 @@ +# please run `bazel run basic_content/const/extern_const:const_file1` and `# please run `bazel run basic_content/const/extern_const:file` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "const_file1", + srcs = ["const_file1.cpp", "const_file2.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "file", + srcs = ["file1.cpp", "file2.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/const/extern_const/const_file1.cpp b/basic_content/const/extern_const/const_file1.cpp index 7077ac9d..a4b7105a 100644 --- a/basic_content/const/extern_const/const_file1.cpp +++ b/basic_content/const/extern_const/const_file1.cpp @@ -1 +1 @@ -extern const int ext=12; +extern const int ext = 12; \ No newline at end of file diff --git a/basic_content/const/extern_const/const_file2.cpp b/basic_content/const/extern_const/const_file2.cpp index 53729f43..8bef46ff 100644 --- a/basic_content/const/extern_const/const_file2.cpp +++ b/basic_content/const/extern_const/const_file2.cpp @@ -1,11 +1,8 @@ -#include +#include /** * by 光城 * compile: g++ -o file const_file2.cpp const_file1.cpp * execute: ./file */ extern const int ext; -int main(){ - - std::cout< +#include /** * by 光城 * compile: g++ -o file file2.cpp file1.cpp * execute: ./file */ extern int ext; -int main(){ - - std::cout<<(ext+10)< +#include using namespace std; - -int main(){ - const int *ptr; - *ptr=10; //error +int main() { + const int *ptr; + *ptr = 10; // error } diff --git a/basic_content/const/funciton_const/condition1/condition2.cpp b/basic_content/const/funciton_const/condition1/condition2.cpp index 9db73393..12a7424b 100644 --- a/basic_content/const/funciton_const/condition1/condition2.cpp +++ b/basic_content/const/funciton_const/condition1/condition2.cpp @@ -1,9 +1,8 @@ -#include +#include using namespace std; - -int main(){ - const int p = 10; - const void *vp = &p; - void *vp = &p; //error +int main() { + const int p = 10; + const void *vp = &p; + void *vp = &p; // error } diff --git a/basic_content/const/funciton_const/condition1/condition3.cpp b/basic_content/const/funciton_const/condition1/condition3.cpp index 5809e5f4..a93da78e 100644 --- a/basic_content/const/funciton_const/condition1/condition3.cpp +++ b/basic_content/const/funciton_const/condition1/condition3.cpp @@ -1,12 +1,11 @@ -#include +#include using namespace std; -int main(){ - const int *ptr; - int val = 3; - ptr = &val; //ok - int *ptr1 = &val; - *ptr1=4; - cout<<*ptr< +#include using namespace std; -int main(){ - - int num=0; - int * const ptr=# //const指针必须初始化!且const指针的值不能修改 - int * t = # - *t = 1; - cout<<*ptr< +#include using namespace std; -int main(){ - - const int num=0; - int * const ptr=# //error! const int* -> int* - cout<<*ptr< int* + cout << *ptr << endl; } diff --git a/basic_content/const/funciton_const/condition2/condition3 b/basic_content/const/funciton_const/condition2/condition3 deleted file mode 100755 index d751dcbb..00000000 Binary files a/basic_content/const/funciton_const/condition2/condition3 and /dev/null differ diff --git a/basic_content/const/funciton_const/condition2/condition3.cpp b/basic_content/const/funciton_const/condition2/condition3.cpp index aed864ce..397d59e2 100644 --- a/basic_content/const/funciton_const/condition2/condition3.cpp +++ b/basic_content/const/funciton_const/condition2/condition3.cpp @@ -1,8 +1,7 @@ -#include +#include using namespace std; -int main(){ - - const int num=10; - const int * const ptr=# //error! const int* -> int* - cout<<*ptr< +#include using namespace std; -int main(){ - - const int p = 3; - const int * const ptr = &p; - cout<<*ptr< #include using namespace std; @@ -14,47 +6,42 @@ using namespace std; */ template -auto multiply(T x, T y)->decltype(x*y) -{ - return x*y; +auto multiply(T x, T y) -> decltype(x * y) { + return x * y; } -int main() -{ - int nums[] = {1,2,3,4}; - vector vec(nums,nums+4); - vector::iterator it; - - for(it=vec.begin();it!=vec.end();it++) - cout<<*it<<" "; - cout< vec(nums, nums + 4); + vector::iterator it; - using nullptr_t = decltype(nullptr); - nullptr_t nu; - int * p =NULL; - if(p==nu) - cout<<"NULL"< -using namespace std; - -enum Color {RED,BLUE}; -enum Feeling {EXCITED,BLUE}; +enum Color +{ + RED, + BLUE +}; +enum Feeling +{ + EXCITED, + BLUE +}; -int main() +int main() { + Color a = BLUE; // error + Feeling b = EXCITED; + std::cout << a << ":" << b << std::endl; return 0; } ``` @@ -106,7 +114,7 @@ char c3 = static_cast(Color3::RED); ## 类中的枚举类型 -有时我们希望某些常量只在类中有效。 由于#define 定义的宏常量是全局的,不能达到目的,于是想到实用const 修饰数据成员来实现。而const 数据成员的确是存在的,但其含义却不是我们所期望的。 +有时我们希望某些常量只在类中有效。 由于#define 定义的宏常量是全局的,不能达到目的,于是想到使用const 修饰数据成员来实现。而const 数据成员的确是存在的,但其含义却不是我们所期望的。 const 数据成员只在某个对象生存期内是常量,而对于整个类而言却是可变的,因为类可以创建多个对象,不同的对象其 const 数据成员的值可以不同。 diff --git a/basic_content/enum/classic_practice b/basic_content/enum/classic_practice deleted file mode 100755 index f9f67b7a..00000000 Binary files a/basic_content/enum/classic_practice and /dev/null differ diff --git a/basic_content/enum/classic_practice.cpp b/basic_content/enum/classic_practice.cpp index f8315217..b0d3a936 100644 --- a/basic_content/enum/classic_practice.cpp +++ b/basic_content/enum/classic_practice.cpp @@ -1,24 +1,10 @@ -/** - * @file classic_practice.cpp - * @brief g++ -o classic_practice classic_practice.cpp -std=c++11 - * @author 光城 - * @version v1 - * @date 2019-08-07 - */ - #include using namespace std; /** * @brief namespace解决作用域不受限 */ -namespace Color -{ - enum Type - { - RED=15, - YELLOW, - BLUE - }; +namespace Color { +enum Type { RED = 15, YELLOW, BLUE }; }; /** @@ -32,56 +18,40 @@ namespace Color * 这里用结构体而非类,一是因为本身希望这些常量可以公开访问, * 二是因为它只包含数据没有成员函数。 */ -struct Color1 -{ - enum Type - { - RED=102, - YELLOW, - BLUE - }; +struct Color1 { + enum Type { RED = 102, YELLOW, BLUE }; }; /** * @brief C++11的枚举类 * 下面等价于enum class Color2:int */ -enum class Color2 -{ - RED=2, - YELLOW, - BLUE -}; +enum class Color2 { RED = 2, YELLOW, BLUE }; -enum class Color3:char; // 前向声明 +enum class Color3 : char; // 前向声明 // 定义 -enum class Color3:char -{ - RED='r', - BLUE -}; +enum class Color3 : char { RED = 'r', BLUE }; + +int main() { + // 定义新的枚举变量 + Color::Type c = Color::RED; + cout << c << endl; + /** + * 上述的另一种方法: + * using namespace Color; // 定义新的枚举变量 + * Type c = RED; + */ + Color1 c1; + cout << c1.RED << endl; + + Color1::Type c11 = Color1::BLUE; + cout << c11 << endl; -int main() -{ - // 定义新的枚举变量 - Color::Type c = Color::RED; - cout<(c2) << endl; - Color1::Type c11 = Color1::BLUE; - cout<(c2) << endl; - - char c3 = static_cast(Color3::RED); - cout<(Color3::RED); + cout << c3 << endl; + return 0; } diff --git a/basic_content/enum/tradition_color.cpp b/basic_content/enum/tradition_color.cpp index 2136b327..8325253f 100644 --- a/basic_content/enum/tradition_color.cpp +++ b/basic_content/enum/tradition_color.cpp @@ -1,10 +1,12 @@ #include using namespace std; -enum Color {RED,BLUE}; -enum Feeling {EXCITED,BLUE}; +enum Color { RED, BLUE }; +enum Feeling { EXCITED, BLUE }; -int main() -{ - return 0; +int main() { + Color a = BLUE; // error + Feeling b = EXCITED; + std::cout << a << ":" << b << std::endl; + return 0; } diff --git a/basic_content/explicit/BUILD b/basic_content/explicit/BUILD new file mode 100644 index 00000000..4017d0f4 --- /dev/null +++ b/basic_content/explicit/BUILD @@ -0,0 +1,9 @@ +# please run `bazel run basic_content/explicit:explicit` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +# Don't panic if you get compilation errors, this is what this code demonstrates, as expected. +cc_binary( + name = "explicit", + srcs = ["explicit.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/explicit/explicit b/basic_content/explicit/explicit deleted file mode 100755 index ab3a4124..00000000 Binary files a/basic_content/explicit/explicit and /dev/null differ diff --git a/basic_content/explicit/explicit.cpp b/basic_content/explicit/explicit.cpp index f6596359..2fe212cf 100644 --- a/basic_content/explicit/explicit.cpp +++ b/basic_content/explicit/explicit.cpp @@ -2,45 +2,50 @@ using namespace std; -struct A -{ - A(int) { } - operator bool() const { return true; } +struct A { + A(int) {} + operator bool() const { return true; } }; -struct B -{ - explicit B(int) {} - explicit operator bool() const { return true; } +struct B { + explicit B(int) {} + explicit operator bool() const { return true; } }; void doA(A a) {} void doB(B b) {} -int main() -{ - A a1(1); // OK:直接初始化 - A a2 = 1; // OK:复制初始化 - A a3{ 1 }; // OK:直接列表初始化 - A a4 = { 1 }; // OK:复制列表初始化 - A a5 = (A)1; // OK:允许 static_cast 的显式转换 - doA(1); // OK:允许从 int 到 A 的隐式转换 - if (a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 - bool a6(a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 - bool a7 = a1; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 - bool a8 = static_cast(a1); // OK :static_cast 进行直接初始化 - - B b1(1); // OK:直接初始化 -// B b2 = 1; // 错误:被 explicit 修饰构造函数的对象不可以复制初始化 - B b3{ 1 }; // OK:直接列表初始化 -// B b4 = { 1 }; // 错误:被 explicit 修饰构造函数的对象不可以复制列表初始化 - B b5 = (B)1; // OK:允许 static_cast 的显式转换 -// doB(1); // 错误:被 explicit 修饰构造函数的对象不可以从 int 到 B 的隐式转换 - if (b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 - bool b6(b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 -// bool b7 = b1; // 错误:被 explicit 修饰转换函数 B::operator bool() 的对象不可以隐式转换 - bool b8 = static_cast(b1); // OK:static_cast 进行直接初始化 - - return 0; +int main() { + A a1(1); // OK:直接初始化 + A a2 = 1; // OK:复制初始化 + A a3{1}; // OK:直接列表初始化 + A a4 = {1}; // OK:复制列表初始化 + A a5 = (A)1; // OK:允许 static_cast 的显式转换 + doA(1); // OK:允许从 int 到 A 的隐式转换 + if (a1) + ; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a6(a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a7 = a1; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a8 = static_cast(a1); // OK :static_cast 进行直接初始化 + + B b1(1); // OK:直接初始化 + // B b2 = 1; // 错误:被 explicit + // 修饰构造函数的对象不可以复制初始化 + B b3{1}; // OK:直接列表初始化 + // B b4 = { 1 }; // 错误:被 explicit + // 修饰构造函数的对象不可以复制列表初始化 + B b5 = (B)1; // OK:允许 static_cast 的显式转换 + // doB(1); // 错误:被 explicit + // 修饰构造函数的对象不可以从 int 到 B 的隐式转换 + if (b1) + ; // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool + // 的按语境转换 + bool b6(b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B + // 到 bool 的按语境转换 + // bool b7 = b1; // 错误:被 explicit 修饰转换函数 + // B::operator bool() 的对象不可以隐式转换 + bool b8 = static_cast(b1); // OK:static_cast 进行直接初始化 + + return 0; } diff --git a/basic_content/extern/README.md b/basic_content/extern/README.md index dd75f50c..215d2015 100644 --- a/basic_content/extern/README.md +++ b/basic_content/extern/README.md @@ -82,7 +82,7 @@ extern "C" { #include "add.h" } int main() { - add(2,3); + std::cout << add(2, 3) << std::endl; return 0; } ``` @@ -99,6 +99,8 @@ gcc -c add.c g++ add.cpp add.o -o main ``` +如果使用bazel编译,运行:`bazel run basic_content/extern/extern_c++:main` + 而通常为了C代码能够通用,即既能被C调用,又能被C++调用,头文件通常会有如下写法: ```c diff --git a/basic_content/extern/extern_c++/BUILD b/basic_content/extern/extern_c++/BUILD new file mode 100644 index 00000000..cfc2abe9 --- /dev/null +++ b/basic_content/extern/extern_c++/BUILD @@ -0,0 +1,15 @@ +# please run `bazel run basic_content/extern/extern_c++:main` +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") + +cc_library( + name = "add", + srcs = ["add.c"], + hdrs = ["add.h"], +) + +cc_binary( + name = "main", + srcs = ["add.cpp"], + deps = [":add"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/extern/extern_c++/add.cpp b/basic_content/extern/extern_c++/add.cpp index 8ed76fa8..492f8483 100644 --- a/basic_content/extern/extern_c++/add.cpp +++ b/basic_content/extern/extern_c++/add.cpp @@ -1,9 +1,9 @@ #include using namespace std; extern "C" { - #include "add.h" +#include "add.h" } int main() { - add(2,3); - return 0; + std::cout << add(2, 3) << std::endl; + return 0; } diff --git a/basic_content/extern/extern_c++/add.h b/basic_content/extern/extern_c++/add.h index 6984cdf6..41c359e7 100644 --- a/basic_content/extern/extern_c++/add.h +++ b/basic_content/extern/extern_c++/add.h @@ -1,4 +1,4 @@ #ifndef ADD_H #define ADD_H -extern int add(int x,int y); +extern int add(int x, int y); #endif diff --git a/basic_content/extern/extern_c++/add.o b/basic_content/extern/extern_c++/add.o deleted file mode 100644 index 4c8c0166..00000000 Binary files a/basic_content/extern/extern_c++/add.o and /dev/null differ diff --git a/basic_content/extern/extern_c++/main b/basic_content/extern/extern_c++/main deleted file mode 100755 index 451dd010..00000000 Binary files a/basic_content/extern/extern_c++/main and /dev/null differ diff --git a/basic_content/extern/extern_c/BUILD b/basic_content/extern/extern_c/BUILD new file mode 100644 index 00000000..9fea1120 --- /dev/null +++ b/basic_content/extern/extern_c/BUILD @@ -0,0 +1,14 @@ +# please run `bazel run basic_content/extern/extern_c:main` +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") + +cc_library( + name = "add", + srcs = ["add.cpp"], + hdrs = ["add.h"], +) + +cc_binary( + name = "main", + srcs = ["add.c"], + deps = [":add"], +) \ No newline at end of file diff --git a/basic_content/extern/extern_c/add.cpp b/basic_content/extern/extern_c/add.cpp index b347468b..627b00af 100644 --- a/basic_content/extern/extern_c/add.cpp +++ b/basic_content/extern/extern_c/add.cpp @@ -1,5 +1,3 @@ #include "add.h" -int add(int x,int y) { - return x+y; -} +int add(int x, int y) { return x + y; } diff --git a/basic_content/extern/extern_c/add.h b/basic_content/extern/extern_c/add.h index 719b7c30..36b84282 100644 --- a/basic_content/extern/extern_c/add.h +++ b/basic_content/extern/extern_c/add.h @@ -1,6 +1,6 @@ #ifndef ADD_H #define ADD_H extern "C" { - int add(int x,int y); +int add(int x, int y); } #endif diff --git a/basic_content/extern/extern_c/add.o b/basic_content/extern/extern_c/add.o deleted file mode 100644 index 564710eb..00000000 Binary files a/basic_content/extern/extern_c/add.o and /dev/null differ diff --git a/basic_content/extern/extern_c/main b/basic_content/extern/extern_c/main deleted file mode 100755 index f026f11a..00000000 Binary files a/basic_content/extern/extern_c/main and /dev/null differ diff --git a/basic_content/friend/BUILD b/basic_content/friend/BUILD new file mode 100644 index 00000000..d3f5b85b --- /dev/null +++ b/basic_content/friend/BUILD @@ -0,0 +1,15 @@ +# please run `bazel run basic_content/friend:friend_class` +# please run `bazel run basic_content/friend:friend_func` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "friend_class", + srcs = ["friend_class.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "friend_func", + srcs = ["friend_func.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/friend/friend_class b/basic_content/friend/friend_class deleted file mode 100755 index edc46292..00000000 Binary files a/basic_content/friend/friend_class and /dev/null differ diff --git a/basic_content/friend/friend_class.cpp b/basic_content/friend/friend_class.cpp index bfa2372f..b5857db1 100644 --- a/basic_content/friend/friend_class.cpp +++ b/basic_content/friend/friend_class.cpp @@ -2,27 +2,23 @@ using namespace std; -class A -{ +class A { public: - A(int _a):a(_a){}; - friend class B; + A(int _a) : a(_a){}; + friend class B; + private: - int a; + int a; }; -class B -{ +class B { public: - int getb(A ca) { - return ca.a; - }; + int getb(A ca) { return ca.a; }; }; -int main() -{ - A a(3); - B b; - cout< using namespace std; -class A -{ +class A { public: - A(int _a):a(_a){}; - friend int geta(A &ca); ///< 友元函数 + A(int _a) : a(_a){}; + friend int geta(A &ca); ///< 友元函数 private: - int a; + int a; }; -int geta(A &ca) -{ - return ca.a; -} +int geta(A &ca) { return ca.a; } -int main() -{ - A a(3); - cout< +#include using namespace std; /** - * @brief 定义了一个变量pFun,这个变量是个指针,指向返回值为空和参数为int的函数的指针! + * @brief + * 定义了一个变量pFun,这个变量是个指针,指向返回值为空和参数为int的函数的指针! */ -void (*pFun)(int); +void (*pFun)(int); /** * @brief 代表一种新类型,不是变量!所以与上述的pFun不一样! */ -typedef void (*func)(void); +typedef void (*func)(void); -void myfunc(void) -{ - cout<<"asda"< #include "inline.h" - - +#include using namespace std; /** - * @brief inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字” + * @brief + * inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字” * * @param x * @param y * - * @return + * @return */ -int Foo(int x,int y); // 函数声明 -inline int Foo(int x,int y) // 函数定义 +int Foo(int x, int y); // 函数声明 +inline int Foo(int x, int y) // 函数定义 { - return x+y; + return x + y; } - - // 定义处加inline关键字,推荐这种写法! -inline void A::f1(int x){ - - -} - - +inline void A::f1(int x) {} /** - * @brief 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。 + * @brief + * 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。 * 如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收货会更少!另一方面,每一处内联函数的调用都要复制代码,将使程序的总代码量增大,消耗更多的内存空间。 * 以下情况不宜用内联: * (1) 如果函数体内的代码比较长,使得内联将导致内存消耗代价比较高。 * (2) 如果函数体内出现循环,那么执行函数体内代码的时间要比函数调用的开销大。 * - * @return + * @return */ -int main() -{ - - - cout< +#include using namespace std; -class Base -{ - public: - inline virtual void who() - { - cout << "I am Base\n"; - } - virtual ~Base() {} +class Base { +public: + inline virtual void who() { cout << "I am Base\n"; } + virtual ~Base() {} }; -class Derived : public Base -{ - public: - inline void who() // 不写inline时隐式内联 - { - cout << "I am Derived\n"; - } +class Derived : public Base { +public: + inline void who() // 不写inline时隐式内联 + { + cout << "I am Derived\n"; + } }; -int main() -{ - // 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 - Base b; - b.who(); +int main() { + // 此处的虚函数 + // who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 + Base b; + b.who(); - // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 - Base *ptr = new Derived(); - ptr->who(); + // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 + Base *ptr = new Derived(); + ptr->who(); - // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 - delete ptr; + // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete + // 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 + delete ptr; - return 0; -} + return 0; +} diff --git a/basic_content/macro/BUILD b/basic_content/macro/BUILD new file mode 100644 index 00000000..9fd999fb --- /dev/null +++ b/basic_content/macro/BUILD @@ -0,0 +1,15 @@ +# please run `bazel run basic_content/macro:do_while` +# please run `bazel run basic_content/sig_examp:sig_examp` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "do_while", + srcs = ["do_while.cpp"], + copts = ["-std=c++11"], +) + +cc_binary( + name = "sig_examp", + srcs = ["sig_examp.cpp"], + copts = ["-std=c++11"], +) diff --git a/basic_content/macro/do_while b/basic_content/macro/do_while deleted file mode 100755 index ce870355..00000000 Binary files a/basic_content/macro/do_while and /dev/null differ diff --git a/basic_content/macro/do_while.cpp b/basic_content/macro/do_while.cpp index 68af1326..b13eaaf6 100644 --- a/basic_content/macro/do_while.cpp +++ b/basic_content/macro/do_while.cpp @@ -2,75 +2,78 @@ #include using namespace std; -#define f1() cout<<"f1()"<0) - fun(); + if (1 > 0) + fun(); - if(2>0) - fun1(); + if (2 > 0) + fun1(); - f(); - ff(); - fc(); - return 0; + f(); + ff(); + fc(); + return 0; } diff --git a/basic_content/macro/sig_examp b/basic_content/macro/sig_examp deleted file mode 100755 index b3423603..00000000 Binary files a/basic_content/macro/sig_examp and /dev/null differ diff --git a/basic_content/macro/sig_examp.cpp b/basic_content/macro/sig_examp.cpp index 85290a72..dfb13ec0 100644 --- a/basic_content/macro/sig_examp.cpp +++ b/basic_content/macro/sig_examp.cpp @@ -1,7 +1,7 @@ -#include -#include #include +#include #include +#include using namespace std; @@ -10,77 +10,73 @@ using namespace std; * (#)字符串操作符 */ ///=========================================== -#define exp(s) printf("test s is:%s\n",s) -#define exp1(s) printf("test s is:%s\n",#s) - -#define exp2(s) #s +#define exp(s) printf("test s is:%s\n", s) +#define exp1(s) printf("test s is:%s\n", #s) +#define exp2(s) #s ///=========================================== /** *(##)符号连接操作符 */ ///=========================================== -#define expA(s) printf("前缀加上后的字符串为:%s\n",gc_##s) //gc_s必须存在 +#define expA(s) printf("前缀加上后的字符串为:%s\n", gc_##s) // gc_s必须存在 -#define expB(s) printf("前缀加上后的字符串为:%s\n",gc_ ## s) //gc_s必须存在 +#define expB(s) printf("前缀加上后的字符串为:%s\n", gc_##s) // gc_s必须存在 #define gc_hello1 "I am gc_hello1" - ///=========================================== /** * (\)续行操作符 */ ///=========================================== -#define MAX(a,b) ((a)>(b) ? (a) \ - :(b)) - -int main() { - ///=========================================== - /** - * (#)字符串操作符 - */ - ///=========================================== - exp("hello"); - exp1(hello); - - string str = exp2( bac ); - cout< (b) ? (a) : (b)) + +int main() { + ///=========================================== + /** + * (#)字符串操作符 + */ + ///=========================================== + exp("hello"); + exp1(hello); + + string str = exp2(bac); + cout << str << " " << str.size() << endl; + /** + * 忽略传入参数名前面和后面的空格。 + */ + string str1 = exp2(asda bac); + /** + * 当传入参数名间存在空格时,编译器将会自动连接各个子字符串, + * 用每个子字符串之间以一个空格连接,忽略剩余空格。 + */ + cout << str1 << " " << str1.size() << endl; + + ///=========================================== + /** + * (#)字符串操作符 + */ + ///=========================================== + + const char *gc_hello = "I am gc_hello"; + expA(hello); + expB(hello1); + + char var1_p[20]; + char var2_p[20]; + + // 连接后的实际参数名赋值 + strcpy(var1_p, "aaaa"); + strcpy(var2_p, "bbbb"); + + ///=========================================== + /** + * (\)续行操作符 + */ + ///=========================================== + int max_val = MAX(3, 6); + cout << max_val << endl; + return 0; } diff --git a/basic_content/maohao/.README.md.un~ b/basic_content/maohao/.README.md.un~ deleted file mode 100644 index a02eba88..00000000 Binary files a/basic_content/maohao/.README.md.un~ and /dev/null differ diff --git a/basic_content/maohao/BUILD b/basic_content/maohao/BUILD new file mode 100644 index 00000000..2abf7590 --- /dev/null +++ b/basic_content/maohao/BUILD @@ -0,0 +1,8 @@ +# please run `bazel run basic_content/maohao:maohao` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "maohao", + srcs = ["maohao.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/maohao/README.md~ b/basic_content/maohao/README.md~ deleted file mode 100644 index 228d6278..00000000 --- a/basic_content/maohao/README.md~ +++ /dev/null @@ -1,14 +0,0 @@ -# :: 范围解析运算符那些事 - -## 关于作者: - -个人公众号: - -![](../img/wechat.jpg) - -- 全局作用域符(::name):用于类型名称(类、类成员、成员函数、变量等)前,表示作用域为全局命名空间 -- 类作用域符(class::name):用于表示指定类型的作用域范围是具体某个类的 -- 命名空间作用域符(namespace::name):用于表示指定类型的作用域范围是具体某个命名空间的 - -具体代码见:[::.cpp](::.cpp) - diff --git a/basic_content/maohao/maohao.cpp b/basic_content/maohao/maohao.cpp index 3460e76e..1aab3b8e 100644 --- a/basic_content/maohao/maohao.cpp +++ b/basic_content/maohao/maohao.cpp @@ -1,20 +1,19 @@ #include using namespace std; -int count=0; // 全局(::)的count +int count = 0; // 全局(::)的count class A { public: - static int count; // 类A的count (A::count) - + static int count; // 类A的count (A::count) }; // 静态变量必须在此处定义 int A::count; int main() { - ::count=1; // 设置全局的count为1 - A::count=5; // 设置类A的count为2 - cout< +#include using namespace std; -void test1(int* p) -{ - *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 - return; +void test1(int *p) { + *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 + return; } -void test2(int& p) -{ - p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 - return; +void test2(int &p) { + p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 + return; } int main() { - int a=10; - int *p=&a; - test1(p); - test2(a); - cout< -#include +#include +#include using namespace std; -#include // use size in graphics also -class A{}; -int main() -{ - cout< - +#include using namespace std; -class A -{ - public: - A(); - ~A(); - static int a; - static void fun3(); - void fun(); - void fun1(); +class A { +public: + A(); + ~A(); + static int a; + static void fun3(); + void fun(); + void fun1(); }; -int main() -{ - cout< +#include using namespace std; -class A -{ - public: - char a; - int b; +class A { +public: + char a; + int b; }; /** @@ -34,39 +33,32 @@ class A * long b * 根据字节对齐2+2+4+8=16 */ -class B:A -{ - public: - short a; - long b; +class B : A { +public: + short a; + long b; }; /** -* 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐 -*/ -class C -{ - A a; - char c; + * 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐 + */ +class C { + A a; + char c; }; -class A1 -{ - virtual void fun(){} -}; -class C1:public A -{ +class A1 { + virtual void fun() {} }; +class C1 : public A {}; +int main() { + cout << sizeof(A) << endl; // 8 + cout << sizeof(B) << endl; // 16 + cout << sizeof(C) << endl; // 12 -int main() -{ - cout< +#include using namespace std; -class A -{ - public: - char a; - int b; +class A { +public: + char a; + int b; }; -class B -{ - public: - short a; - long b; +class B { +public: + short a; + long b; }; /** * @brief 8+16+8=32 */ -class C:A,B -{ - char c; +class C : A, B { + char c; }; +int main() { + cout << sizeof(A) << endl; // 8 + cout << sizeof(B) << endl; // 16 + cout << sizeof(C) << endl; // 32 -int main() -{ - cout< +#include using namespace std; -class A{ +class A { - virtual void fun(); - virtual void fun1(); - virtual void fun2(); - virtual void fun3(); + virtual void fun(); + virtual void fun1(); + virtual void fun2(); + virtual void fun3(); }; -int main() -{ - cout< +#include using namespace std; -class A -{ - public: - char b; - virtual void fun() {}; - static int c; - static int d; - static int f; +class A { +public: + char b; + virtual void fun(){}; + static int c; + static int d; + static int f; }; +int main() { + /** + * @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8 + */ + cout << sizeof(A) << endl; -int main() -{ - - /** - * @brief 16 字节对齐、静态变量不影响类的大小、vptr指针=8 - */ - cout< +#include using namespace std; -class A -{ - virtual void fun() {} +class A { + virtual void fun() {} }; -class B -{ - virtual void fun2() {} +class B { + virtual void fun2() {} }; -class C : virtual public A, virtual public B -{ - public: - virtual void fun3() {} +class C : virtual public A, virtual public B { +public: + virtual void fun3() {} }; +int main() { -int main() -{ + /** + * @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr + */ + cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C); - /** - * @brief 8 8 16 派生类虚继承多个虚函数,会继承所有虚函数的vptr - */ - cout< +#include using namespace std; -class A -{ - virtual void fun() {} +class A { + virtual void fun() {} }; -class B -{ - virtual void fun2() {} +class B { + virtual void fun2() {} }; -class C : public A, public B -{ - public: - virtual void fun3() {} +class C : public A, public B { +public: + virtual void fun3() {} }; +int main() { -int main() -{ + /** + * @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr + */ + cout << sizeof(A) << " " << sizeof(B) << " " << sizeof(C); - /** - * @brief 8 8 16 派生类继承多个虚函数,会继承所有虚函数的vptr - */ - cout< -using namespace std; +#include +using namespace std; -class Apple -{ - int i; - public: - Apple() - { - i = 0; - cout << "Inside Constructor\n"; - } - ~Apple() - { - cout << "Inside Destructor\n"; - } -}; +class Apple { + int i; -int main() -{ - int x = 0; - if (x==0) - { - Apple obj; - } - cout << "End of main\n"; -} +public: + Apple() { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() { cout << "Inside Destructor\n"; } +}; +int main() { + int x = 0; + if (x == 0) { + Apple obj; + } + cout << "End of main\n"; +} diff --git a/basic_content/static/static_class.cpp b/basic_content/static/static_class.cpp index 007e400e..81d4f103 100644 --- a/basic_content/static/static_class.cpp +++ b/basic_content/static/static_class.cpp @@ -1,28 +1,21 @@ -#include -using namespace std; +#include +using namespace std; -class Apple -{ - int i; - public: - Apple() - { - i = 0; - cout << "Inside Constructor\n"; - } - ~Apple() - { - cout << "Inside Destructor\n"; - } -}; +class Apple { + int i; -int main() -{ - int x = 0; - if (x==0) - { - static Apple obj; - } - cout << "End of main\n"; -} +public: + Apple() { + i = 0; + cout << "Inside Constructor\n"; + } + ~Apple() { cout << "Inside Destructor\n"; } +}; +int main() { + int x = 0; + if (x == 0) { + static Apple obj; + } + cout << "End of main\n"; +} diff --git a/basic_content/static/static_demo.cpp b/basic_content/static/static_demo.cpp index 19100889..b07920cf 100644 --- a/basic_content/static/static_demo.cpp +++ b/basic_content/static/static_demo.cpp @@ -1,24 +1,22 @@ -// the use of static Static -// variables in a Function -#include -#include -using namespace std; +// the use of static Static +// variables in a Function +#include +#include +using namespace std; -void demo() -{ - // static variable - static int count = 0; - cout << count << " "; +void demo() { + // static variable + static int count = 0; + cout << count << " "; - // value is updated and - // will be carried to next - // function calls - count++; -} + // value is updated and + // will be carried to next + // function calls + count++; +} -int main() -{ - for (int i=0; i<5; i++) - demo(); - return 0; -} +int main() { + for (int i = 0; i < 5; i++) + demo(); + return 0; +} diff --git a/basic_content/static/static_error_variable.cpp b/basic_content/static/static_error_variable.cpp index 47b13194..76ba4610 100644 --- a/basic_content/static/static_error_variable.cpp +++ b/basic_content/static/static_error_variable.cpp @@ -1,26 +1,23 @@ -// variables inside a class +// variables inside a class -#include -using namespace std; +#include +using namespace std; -class Apple -{ - public: - static int i; +class Apple { +public: + static int i; - Apple() - { - // Do nothing - }; -}; + Apple(){ + // Do nothing + }; +}; -int main() -{ - Apple obj1; - Apple obj2; - obj1.i =2; - obj2.i = 3; +int main() { + Apple obj1; + Apple obj2; + obj1.i = 2; + obj2.i = 3; - // prints value of i - cout << obj1.i<<" "< -using namespace std; +#include +using namespace std; -class Apple -{ - public: - // static member function - static void printMsg() - { - cout<<"Welcome to Apple!"; - } -}; - -// main function -int main() -{ - // invoking a static member function - Apple::printMsg(); -} +class Apple { +public: + // static member function + static void printMsg() { cout << "Welcome to Apple!"; } +}; +// main function +int main() { + // invoking a static member function + Apple::printMsg(); +} diff --git a/basic_content/static/static_variable.cpp b/basic_content/static/static_variable.cpp index b14780e8..78389db3 100644 --- a/basic_content/static/static_variable.cpp +++ b/basic_content/static/static_variable.cpp @@ -1,24 +1,21 @@ -// variables inside a class +// variables inside a class -#include -using namespace std; +#include +using namespace std; -class GfG -{ - public: - static int i; +class GfG { +public: + static int i; - GfG() - { - // Do nothing - }; -}; + GfG(){ + // Do nothing + }; +}; -int GfG::i = 1; +int GfG::i = 1; -int main() -{ - GfG obj; - // prints value of i - cout << obj.i; -} +int main() { + GfG obj; + // prints value of i + cout << obj.i; +} diff --git a/basic_content/struct/BUILD b/basic_content/struct/BUILD new file mode 100644 index 00000000..ed8f05e1 --- /dev/null +++ b/basic_content/struct/BUILD @@ -0,0 +1,29 @@ +# please run `bazel run basic_content/struct:ext_struct_func` +# please run `bazel run basic_content/struct:struct_func_func` +# please run `bazel run basic_content/struct:struct_func` +# please run `bazel run basic_content/struct:struct_func_cpp` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "ext_struct_func", + srcs = ["ext_struct_func.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "struct_func_func", + srcs = ["struct_func_func.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "struct_func", + srcs = ["struct_func.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "struct_func_cpp", + srcs = ["struct_func.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/struct/ext b/basic_content/struct/ext deleted file mode 100755 index 45104f89..00000000 Binary files a/basic_content/struct/ext and /dev/null differ diff --git a/basic_content/struct/ext_struct_func.cpp b/basic_content/struct/ext_struct_func.cpp index 7ab1eb23..d189a853 100644 --- a/basic_content/struct/ext_struct_func.cpp +++ b/basic_content/struct/ext_struct_func.cpp @@ -1,33 +1,31 @@ -#include -#include +#include +#include using namespace std; -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - virtual void print(){ - printf("%s\n","Base"); - }; - Base(){cout<<"Base construct"<print(); - delete b; - return 0; + Base *b = new Derived(); + b->print(); + delete b; + return 0; } diff --git a/basic_content/struct/sf b/basic_content/struct/sf deleted file mode 100755 index 321061d2..00000000 Binary files a/basic_content/struct/sf and /dev/null differ diff --git a/basic_content/struct/stff b/basic_content/struct/stff deleted file mode 100755 index de437278..00000000 Binary files a/basic_content/struct/stff and /dev/null differ diff --git a/basic_content/struct/struct_func b/basic_content/struct/struct_func deleted file mode 100755 index 8541eba0..00000000 Binary files a/basic_content/struct/struct_func and /dev/null differ diff --git a/basic_content/struct/struct_func.c b/basic_content/struct/struct_func.c index 6bea643b..83df9a23 100644 --- a/basic_content/struct/struct_func.c +++ b/basic_content/struct/struct_func.c @@ -1,27 +1,30 @@ -#include +#include -struct Base { // public +struct Base +{ // public int v1; -// public: //error - int v2; - //private: - int v3; - //void print(){ // c中不能在结构体中嵌入函数 - // printf("%s\n","hello world"); - //}; //error! + // public: //error + int v2; + // private: + int v3; + // void print(){ // c中不能在结构体中嵌入函数 + // printf("%s\n","hello world"); + // }; //error! }; -void Base(){ - printf("%s\n","I am Base func"); +void Base() +{ + printf("%s\n", "I am Base func"); } -//struct Base base1; //ok -//Base base2; //error +// struct Base base1; //ok +// Base base2; //error -int main() { +int main() +{ struct Base base; - base.v1=1; - //base.print(); - printf("%d\n",base.v1); + base.v1 = 1; + // base.print(); + printf("%d\n", base.v1); Base(); return 0; } diff --git a/basic_content/struct/struct_func.cpp b/basic_content/struct/struct_func.cpp index 09bef266..7153e1d0 100644 --- a/basic_content/struct/struct_func.cpp +++ b/basic_content/struct/struct_func.cpp @@ -1,25 +1,24 @@ -#include -#include +#include +#include -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; +struct Base { + int v1; + // private: //error! + int v3; + +public: // 显示声明public + int v2; + void print() { printf("%s\n", "hello world"); }; }; int main() { - struct Base base1; //ok - Base base2; //ok - Base base; - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - return 0; + struct Base base1; // ok + Base base2; // ok + Base base; + base.v1 = 1; + base.v3 = 2; + base.print(); + printf("%d\n", base.v1); + printf("%d\n", base.v3); + return 0; } diff --git a/basic_content/struct/struct_func_func.cpp b/basic_content/struct/struct_func_func.cpp index bce37499..5ca9e88f 100644 --- a/basic_content/struct/struct_func_func.cpp +++ b/basic_content/struct/struct_func_func.cpp @@ -1,39 +1,35 @@ -#include -#include +#include +#include -struct Base { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; +struct Base { + int v1; + // private: //error! + int v3; + +public: // 显示声明public + int v2; + void print() { printf("%s\n", "hello world"); }; }; -typedef struct Base1 { - int v1; -// private: //error! - int v3; - public: //显示声明public - int v2; - void print(){ - printf("%s\n","hello world"); - }; -}B; -void Base(){ - printf("%s\n","I am Base func"); -} -//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名 +typedef struct Base1 { + int v1; + // private: //error! + int v3; + +public: // 显示声明public + int v2; + void print() { printf("%s\n", "hello world"); }; +} B; +void Base() { printf("%s\n", "I am Base func"); } +// void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名 int main() { - struct Base base; //ok - //Base base1; // error! - base.v1=1; - base.v3=2; - base.print(); - printf("%d\n",base.v1); - printf("%d\n",base.v3); - Base(); - return 0; + struct Base base; // ok + // Base base1; // error! + base.v1 = 1; + base.v3 = 2; + base.print(); + printf("%d\n", base.v1); + printf("%d\n", base.v3); + Base(); + return 0; } diff --git a/basic_content/this/BUILD b/basic_content/this/BUILD new file mode 100644 index 00000000..677f822c --- /dev/null +++ b/basic_content/this/BUILD @@ -0,0 +1,8 @@ +# please run `bazel run basic_content/this:person` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "person", + srcs = ["person.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/this/person b/basic_content/this/person deleted file mode 100755 index 99a540ec..00000000 Binary files a/basic_content/this/person and /dev/null differ diff --git a/basic_content/this/person.cpp b/basic_content/this/person.cpp index 613dab95..512c0ccf 100644 --- a/basic_content/this/person.cpp +++ b/basic_content/this/person.cpp @@ -1,41 +1,32 @@ -#include -#include - +#include +#include using namespace std; -class Person{ +class Person { public: - typedef enum { - BOY = 0, - GIRL - }SexType; - Person(char *n, int a,SexType s){ - name=new char[strlen(n)+1]; - strcpy(name,n); - age=a; - sex=s; - } - int get_age() const{ - - return this->age; - } - Person& add_age(int a){ - age+=a; - return *this; - } - ~Person(){ - delete [] name; - } + typedef enum { BOY = 0, GIRL } SexType; + Person(char *n, int a, SexType s) { + name = new char[strlen(n) + 1]; + strcpy(name, n); + age = a; + sex = s; + } + int get_age() const { return this->age; } + Person &add_age(int a) { + age += a; + return *this; + } + ~Person() { delete[] name; } + private: - char * name; - int age; - SexType sex; + char *name; + int age; + SexType sex; }; +int main() { + Person p("zhangsan", 20, Person::BOY); + cout << p.get_age() << endl; -int main(){ - Person p("zhangsan",20,Person::BOY); - cout< +#include /** * 默认访问控制符为public */ union UnionTest { - /** - * 可以含有构造函数、析构函数 - */ - UnionTest() : i(10) {print(i);}; - ~UnionTest(){}; - int i; + /** + * 可以含有构造函数、析构函数 + */ + UnionTest() : i(10) { print(i); }; + ~UnionTest(){}; + int i; + private: - void print(int i){std::cout< using namespace std; -class Base{ - public: - void f(){ cout<<"f()"< #define isNs1 1 -//#define isGlobal 2 +// #define isGlobal 2 using namespace std; -void func() -{ - cout<<"::func"< V1; using V2 = vector; +int main() { + int nums1[] = {1, 2, 3, 4, 5, 6}; + V1 vec1(nums1, nums1 + sizeof(nums1) / sizeof(int)); + int nums2[] = {5, 7, 6}; + V2 vec2(nums2, nums2 + sizeof(nums2) / sizeof(int)); -int main() -{ - int nums1[] = {1,2,3,4,5,6}; - V1 vec1(nums1,nums1+sizeof(nums1)/sizeof(int)); - int nums2[] = {5,7,6}; - V2 vec2(nums2,nums2+sizeof(nums2)/sizeof(int)); - - for(auto i:vec1) - cout< +#include using namespace std; +class Employee { +public: + virtual void raiseSalary() { cout << 0 << endl; } -class Employee -{ - public: - virtual void raiseSalary() - { - cout<<0<raiseSalary(); // Polymorphic Call: Calls raiseSalary() - // according to the actual object, not - // according to the type of pointer -} -int main(){ - Employee *emp[]={new Manager(),new Engineer}; - globalRaiseSalary(emp,2); - return 0; +// Similarly, there may be other types of employees +// We need a very simple function to increment salary of all employees +// Note that emp[] is an array of pointers and actual pointed objects can +// be any type of employees. This function should ideally be in a class +// like Organization, we have made it global to keep things simple +void globalRaiseSalary(Employee *emp[], int n) { + for (int i = 0; i < n; i++) + emp[i]->raiseSalary(); // Polymorphic Call: Calls raiseSalary() + // according to the actual object, not + // according to the type of pointer +} +int main() { + Employee *emp[] = {new Manager(), new Engineer}; + globalRaiseSalary(emp, 2); + return 0; } diff --git a/basic_content/virtual/set2/BUILD b/basic_content/virtual/set2/BUILD new file mode 100644 index 00000000..bf1816b4 --- /dev/null +++ b/basic_content/virtual/set2/BUILD @@ -0,0 +1,8 @@ +# please run `bazel run basic_content/virtual/set2:default_arg` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "default_arg", + srcs = ["default_arg.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/virtual/set2/default_arg.cpp b/basic_content/virtual/set2/default_arg.cpp index 47978189..3697b930 100644 --- a/basic_content/virtual/set2/default_arg.cpp +++ b/basic_content/virtual/set2/default_arg.cpp @@ -7,33 +7,22 @@ * @date 2019-07-24 */ -#include -using namespace std; +#include +using namespace std; -class Base -{ - public: - virtual void fun ( int x = 10 ) - { - cout << "Base::fun(), x = " << x << endl; - } -}; +class Base { +public: + virtual void fun(int x = 10) { cout << "Base::fun(), x = " << x << endl; } +}; -class Derived : public Base -{ - public: - virtual void fun ( int x=20 ) - { - cout << "Derived::fun(), x = " << x << endl; - } -}; - - -int main() -{ - Derived d1; - Base *bp = &d1; - bp->fun(); // 10 - return 0; -} +class Derived : public Base { +public: + virtual void fun(int x = 20) { cout << "Derived::fun(), x = " << x << endl; } +}; +int main() { + Derived d1; + Base *bp = &d1; + bp->fun(); // 10 + return 0; +} diff --git a/basic_content/virtual/set3/BUILD b/basic_content/virtual/set3/BUILD new file mode 100644 index 00000000..23cefd3a --- /dev/null +++ b/basic_content/virtual/set3/BUILD @@ -0,0 +1,57 @@ +# please run `bazel run //basic_content/virtual/set3:copy_consrtuct` +# please run `bazel run //basic_content/virtual/set3:full_virde` +# please run `bazel run //basic_content/virtual/set3:inline_virtual` +# please run `bazel run //basic_content/virtual/set3:vir_con` +# please run `bazel run //basic_content/virtual/set3:vir_de` +# please run `bazel run //basic_content/virtual/set3:virtual_function` +# please run `bazel run //basic_content/virtual/set3:virtual_function1` +# please run `bazel run //basic_content/virtual/set3:virtual_inline` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "copy_consrtuct", + srcs = ["copy_consrtuct.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "full_virde", + srcs = ["full_virde.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "inline_virtual", + srcs = ["inline_virtual.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "vir_con", + srcs = ["vir_con.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "vir_de", + srcs = ["vir_de.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "virtual_function", + srcs = ["virtual_function.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "virtual_function1", + srcs = ["virtual_function1.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "virtual_inline", + srcs = ["virtual_inline.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/virtual/set3/copy_consrtuct.cpp b/basic_content/virtual/set3/copy_consrtuct.cpp index e65ca897..c2481d6e 100644 --- a/basic_content/virtual/set3/copy_consrtuct.cpp +++ b/basic_content/virtual/set3/copy_consrtuct.cpp @@ -1,41 +1,29 @@ -#include -using namespace std; +#include +using namespace std; -class Base -{ - public: +class Base { +public: +}; -}; +class Derived : public Base { +public: + Derived() { cout << "Derived created" << endl; } -class Derived : public Base -{ - public: - Derived() - { - cout << "Derived created" << endl; - } + Derived(const Derived &rhs) { + cout << "Derived created by deep copy" << endl; + } - Derived(const Derived &rhs) - { - cout << "Derived created by deep copy" << endl; - } + ~Derived() { cout << "Derived destroyed" << endl; } +}; - ~Derived() - { - cout << "Derived destroyed" << endl; - } -}; +int main() { + Derived s1; -int main() -{ - Derived s1; + Derived s2 = s1; // Compiler invokes "copy constructor" + // Type of s1 and s2 are concrete to compiler - Derived s2 = s1; // Compiler invokes "copy constructor" - // Type of s1 and s2 are concrete to compiler - - // How can we create Derived1 or Derived2 object - // from pointer (reference) to Base class pointing Derived object? - - return 0; -} + // How can we create Derived1 or Derived2 object + // from pointer (reference) to Base class pointing Derived object? + return 0; +} diff --git a/basic_content/virtual/set3/full_virde.cpp b/basic_content/virtual/set3/full_virde.cpp index d4ef7a8a..f4f20e50 100644 --- a/basic_content/virtual/set3/full_virde.cpp +++ b/basic_content/virtual/set3/full_virde.cpp @@ -10,30 +10,25 @@ * @version v1 * @date 2019-07-24 */ -#include +#include -using namespace std; +using namespace std; -class base { - public: - base() - { cout<<"Constructing base \n"; } - virtual ~base() - { cout<<"Destructing base \n"; } -}; +class base { +public: + base() { cout << "Constructing base \n"; } + virtual ~base() { cout << "Destructing base \n"; } +}; -class derived: public base { - public: - derived() - { cout<<"Constructing derived \n"; } - ~derived() - { cout<<"Destructing derived \n"; } -}; +class derived : public base { +public: + derived() { cout << "Constructing derived \n"; } + ~derived() { cout << "Destructing derived \n"; } +}; -int main(void) -{ - derived *d = new derived(); - base *b = d; - delete b; - return 0; -} +int main(void) { + derived *d = new derived(); + base *b = d; + delete b; + return 0; +} diff --git a/basic_content/virtual/set3/inline_virtual.cpp b/basic_content/virtual/set3/inline_virtual.cpp index 1ec1a065..be87fccf 100644 --- a/basic_content/virtual/set3/inline_virtual.cpp +++ b/basic_content/virtual/set3/inline_virtual.cpp @@ -1,35 +1,31 @@ -#include +#include using namespace std; -class Base -{ - public: - inline virtual void who() - { - cout << "I am Base\n"; - } - virtual ~Base() {} +class Base { +public: + inline virtual void who() { cout << "I am Base\n"; } + virtual ~Base() {} }; -class Derived : public Base -{ - public: - inline void who() // 不写inline时隐式内联 - { - cout << "I am Derived\n"; - } +class Derived : public Base { +public: + inline void who() // 不写inline时隐式内联 + { + cout << "I am Derived\n"; + } }; -int main() -{ - // 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 - Base b; - b.who(); +int main() { + // 此处的虚函数 + // who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。 + Base b; + b.who(); - // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 - Base *ptr = new Derived(); - ptr->who(); + // 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。 + Base *ptr = new Derived(); + ptr->who(); - // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 - delete ptr; + // 因为Base有虚析构函数(virtual ~Base() {}),所以 delete + // 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。 + delete ptr; - return 0; -} + return 0; +} diff --git a/basic_content/virtual/set3/static_error.cpp b/basic_content/virtual/set3/static_error.cpp index 5a79667c..32f49664 100644 --- a/basic_content/virtual/set3/static_error.cpp +++ b/basic_content/virtual/set3/static_error.cpp @@ -9,5 +9,5 @@ * @date 2019-07-24 */ -virtual static void fun() { } -static void fun() const { } +virtual static void fun() {} +static void fun() const {} diff --git a/basic_content/virtual/set3/vir_con.cpp b/basic_content/virtual/set3/vir_con.cpp index 78c4a07f..c40ee1ee 100644 --- a/basic_content/virtual/set3/vir_con.cpp +++ b/basic_content/virtual/set3/vir_con.cpp @@ -1,206 +1,158 @@ /** * @file vir_con.cpp - * @brief 构造函数不可以声明为虚函数。同时除了inline之外,构造函数不允许使用其它任何关键字。 + * @brief + * 构造函数不可以声明为虚函数。同时除了inline之外,构造函数不允许使用其它任何关键字。 * * 为什么构造函数不可以为虚函数? * - * 尽管虚函数表vtable是在编译阶段就已经建立的,但指向虚函数表的指针vptr是在运行阶段实例化对象时才产生的。 如果类含有虚函数,编译器会在构造函数中添加代码来创建vptr。 问题来了,如果构造函数是虚的,那么它需要vptr来访问vtable,可这个时候vptr还没产生。 因此,构造函数不可以为虚函数。 + * 尽管虚函数表vtable是在编译阶段就已经建立的,但指向虚函数表的指针vptr是在运行阶段实例化对象时才产生的。 + * 如果类含有虚函数,编译器会在构造函数中添加代码来创建vptr。 + * 问题来了,如果构造函数是虚的,那么它需要vptr来访问vtable,可这个时候vptr还没产生。 + * 因此,构造函数不可以为虚函数。 * 我们之所以使用虚函数,是因为需要在信息不全的情况下进行多态运行。而构造函数是用来初始化实例的,实例的类型必须是明确的。 * 因此,构造函数没有必要被声明为虚函数。 - * 尽管构造函数不可以为虚函数,但是有些场景下我们确实需要 “Virtual Copy Constructor”。 “虚复制构造函数”的说法并不严谨,其只是一个实现了对象复制的功能的类内函数。 举一个应用场景,比如剪切板功能。 复制内容作为基类,但派生类可能包含文字、图片、视频等等。 我们只有在程序运行的时候才知道我们需要复制的具体是什么类型的数据。 + * 尽管构造函数不可以为虚函数,但是有些场景下我们确实需要 “Virtual Copy + * Constructor”。 + * “虚复制构造函数”的说法并不严谨,其只是一个实现了对象复制的功能的类内函数。 + * 举一个应用场景,比如剪切板功能。 + * 复制内容作为基类,但派生类可能包含文字、图片、视频等等。 + * 我们只有在程序运行的时候才知道我们需要复制的具体是什么类型的数据。 * * @author 光城 * @version v1 * @date 2019-07-24 */ -#include -using namespace std; - -//// LIBRARY SRART -class Base -{ - public: - Base() { } - - virtual // Ensures to invoke actual object destructor - ~Base() { } - - virtual void ChangeAttributes() = 0; - - // The "Virtual Constructor" - static Base *Create(int id); - - // The "Virtual Copy Constructor" - virtual Base *Clone() = 0; -}; - -class Derived1 : public Base -{ - public: - Derived1() - { - cout << "Derived1 created" << endl; - } - - Derived1(const Derived1& rhs) - { - cout << "Derived1 created by deep copy" << endl; - } - - ~Derived1() - { - cout << "~Derived1 destroyed" << endl; - } - - void ChangeAttributes() - { - cout << "Derived1 Attributes Changed" << endl; - } - - Base *Clone() - { - return new Derived1(*this); - } -}; - -class Derived2 : public Base -{ - public: - Derived2() - { - cout << "Derived2 created" << endl; - } - - Derived2(const Derived2& rhs) - { - cout << "Derived2 created by deep copy" << endl; - } - - ~Derived2() - { - cout << "~Derived2 destroyed" << endl; - } - - void ChangeAttributes() - { - cout << "Derived2 Attributes Changed" << endl; - } - - Base *Clone() - { - return new Derived2(*this); - } -}; - -class Derived3 : public Base -{ - public: - Derived3() - { - cout << "Derived3 created" << endl; - } - - Derived3(const Derived3& rhs) - { - cout << "Derived3 created by deep copy" << endl; - } - - ~Derived3() - { - cout << "~Derived3 destroyed" << endl; - } - - void ChangeAttributes() - { - cout << "Derived3 Attributes Changed" << endl; - } - - Base *Clone() - { - return new Derived3(*this); - } -}; - -// We can also declare "Create" outside Base. -// But is more relevant to limit it's scope to Base -Base *Base::Create(int id) -{ - // Just expand the if-else ladder, if new Derived class is created - // User need not be recompiled to create newly added class objects - - if( id == 1 ) - { - return new Derived1; - } - else if( id == 2 ) - { - return new Derived2; - } - else - { - return new Derived3; - } -} -//// LIBRARY END - -//// UTILITY SRART -class User -{ - public: - User() : pBase(0) - { - // Creates any object of Base heirarchey at runtime - - int input; - - cout << "Enter ID (1, 2 or 3): "; - cin >> input; - - while( (input != 1) && (input != 2) && (input != 3) ) - { - cout << "Enter ID (1, 2 or 3 only): "; - cin >> input; - } - - // Create objects via the "Virtual Constructor" - pBase = Base::Create(input); - } - - ~User() - { - if( pBase ) - { - delete pBase; - pBase = 0; - } - } - - void Action() - { - // Duplicate current object - Base *pNewBase = pBase->Clone(); - - // Change its attributes - pNewBase->ChangeAttributes(); - - // Dispose the created object - delete pNewBase; - } - - private: - Base *pBase; -}; - -//// UTILITY END - -//// Consumer of User (UTILITY) class -int main() -{ - User *user = new User(); - - user->Action(); - - delete user; -} +#include +using namespace std; +//// LIBRARY SRART +class Base { +public: + Base() {} + + virtual // Ensures to invoke actual object destructor + ~Base() {} + + virtual void ChangeAttributes() = 0; + + // The "Virtual Constructor" + static Base *Create(int id); + + // The "Virtual Copy Constructor" + virtual Base *Clone() = 0; +}; + +class Derived1 : public Base { +public: + Derived1() { cout << "Derived1 created" << endl; } + + Derived1(const Derived1 &rhs) { + cout << "Derived1 created by deep copy" << endl; + } + + ~Derived1() { cout << "~Derived1 destroyed" << endl; } + + void ChangeAttributes() { cout << "Derived1 Attributes Changed" << endl; } + + Base *Clone() { return new Derived1(*this); } +}; + +class Derived2 : public Base { +public: + Derived2() { cout << "Derived2 created" << endl; } + + Derived2(const Derived2 &rhs) { + cout << "Derived2 created by deep copy" << endl; + } + + ~Derived2() { cout << "~Derived2 destroyed" << endl; } + + void ChangeAttributes() { cout << "Derived2 Attributes Changed" << endl; } + + Base *Clone() { return new Derived2(*this); } +}; + +class Derived3 : public Base { +public: + Derived3() { cout << "Derived3 created" << endl; } + + Derived3(const Derived3 &rhs) { + cout << "Derived3 created by deep copy" << endl; + } + + ~Derived3() { cout << "~Derived3 destroyed" << endl; } + + void ChangeAttributes() { cout << "Derived3 Attributes Changed" << endl; } + + Base *Clone() { return new Derived3(*this); } +}; + +// We can also declare "Create" outside Base. +// But is more relevant to limit it's scope to Base +Base *Base::Create(int id) { + // Just expand the if-else ladder, if new Derived class is created + // User need not be recompiled to create newly added class objects + + if (id == 1) { + return new Derived1; + } else if (id == 2) { + return new Derived2; + } else { + return new Derived3; + } +} +//// LIBRARY END + +//// UTILITY SRART +class User { +public: + User() : pBase(0) { + // Creates any object of Base heirarchey at runtime + + int input; + + cout << "Enter ID (1, 2 or 3): "; + cin >> input; + + while ((input != 1) && (input != 2) && (input != 3)) { + cout << "Enter ID (1, 2 or 3 only): "; + cin >> input; + } + + // Create objects via the "Virtual Constructor" + pBase = Base::Create(input); + } + + ~User() { + if (pBase) { + delete pBase; + pBase = 0; + } + } + + void Action() { + // Duplicate current object + Base *pNewBase = pBase->Clone(); + + // Change its attributes + pNewBase->ChangeAttributes(); + + // Dispose the created object + delete pNewBase; + } + +private: + Base *pBase; +}; + +//// UTILITY END + +//// Consumer of User (UTILITY) class +int main() { + User *user = new User(); + + user->Action(); + + delete user; +} diff --git a/basic_content/virtual/set3/vir_de.cpp b/basic_content/virtual/set3/vir_de.cpp index 79c7c656..ae8fbd47 100644 --- a/basic_content/virtual/set3/vir_de.cpp +++ b/basic_content/virtual/set3/vir_de.cpp @@ -10,32 +10,27 @@ * @date 2019-07-24 */ -// CPP program without virtual destructor -// causing undefined behavior -#include +// CPP program without virtual destructor +// causing undefined behavior +#include -using namespace std; +using namespace std; -class base { - public: - base() - { cout<<"Constructing base \n"; } - ~base() - { cout<<"Destructing base \n"; } -}; +class base { +public: + base() { cout << "Constructing base \n"; } + ~base() { cout << "Destructing base \n"; } +}; -class derived: public base { - public: - derived() - { cout<<"Constructing derived \n"; } - ~derived() - { cout<<"Destructing derived \n"; } -}; +class derived : public base { +public: + derived() { cout << "Constructing derived \n"; } + ~derived() { cout << "Destructing derived \n"; } +}; -int main(void) -{ - derived *d = new derived(); - base *b = d; - delete b; - return 0; -} +int main(void) { + derived *d = new derived(); + base *b = d; + delete b; + return 0; +} diff --git a/basic_content/virtual/set3/virtual_function.cpp b/basic_content/virtual/set3/virtual_function.cpp index 1b6d039a..1c98adaa 100644 --- a/basic_content/virtual/set3/virtual_function.cpp +++ b/basic_content/virtual/set3/virtual_function.cpp @@ -2,32 +2,32 @@ * @file virtual_function.cpp * @brief 虚函数可以被私有化,但有一些细节需要注意。 * 基类指针指向继承类对象,则调用继承类对象的函数; - * int main()必须声明为Base类的友元,否则编译失败。 编译器报错: ptr无法访问私有函数。 - * 当然,把基类声明为public, 继承类为private,该问题就不存在了。----> 见另外一个例子! + * int main()必须声明为Base类的友元,否则编译失败。 编译器报错: + * ptr无法访问私有函数。 当然,把基类声明为public, + * 继承类为private,该问题就不存在了。----> 见另外一个例子! * @author 光城 * @version v1 * @date 2019-07-24 */ -#include -using namespace std; +#include +using namespace std; -class Derived; +class Derived; -class Base { - private: - virtual void fun() { cout << "Base Fun"; } - friend int main(); -}; +class Base { +private: + virtual void fun() { cout << "Base Fun"; } + friend int main(); +}; -class Derived: public Base { - public: - void fun() { cout << "Derived Fun"; } -}; +class Derived : public Base { +public: + void fun() { cout << "Derived Fun"; } +}; -int main() -{ - Base *ptr = new Derived; - ptr->fun(); - return 0; +int main() { + Base *ptr = new Derived; + ptr->fun(); + return 0; } diff --git a/basic_content/virtual/set3/virtual_function1.cpp b/basic_content/virtual/set3/virtual_function1.cpp index 27648e11..f893b649 100644 --- a/basic_content/virtual/set3/virtual_function1.cpp +++ b/basic_content/virtual/set3/virtual_function1.cpp @@ -1,22 +1,21 @@ -#include -using namespace std; +#include +using namespace std; -class Derived; +class Derived; -class Base { - public: - virtual void fun() { cout << "Base Fun"; } - // friend int main(); -}; +class Base { +public: + virtual void fun() { cout << "Base Fun"; } + // friend int main(); +}; -class Derived: public Base { - private: - void fun() { cout << "Derived Fun"; } -}; +class Derived : public Base { +private: + void fun() { cout << "Derived Fun"; } +}; -int main() -{ - Base *ptr = new Derived; - ptr->fun(); - return 0; +int main() { + Base *ptr = new Derived; + ptr->fun(); + return 0; } diff --git a/basic_content/virtual/set3/virtual_inline.cpp b/basic_content/virtual/set3/virtual_inline.cpp index ac899647..ed21a628 100644 --- a/basic_content/virtual/set3/virtual_inline.cpp +++ b/basic_content/virtual/set3/virtual_inline.cpp @@ -1,6 +1,6 @@ /** * @file virtual_inline.cpp - * @brief 通常类成员函数都会被编译器考虑是否进行内联。 + * @brief 通常类成员函数都会被编译器考虑是否进行内联。 * 但通过基类指针或者引用调用的虚函数必定不能被内联。 * 当然,实体对象调用虚函数或者静态调用时可以被内联,虚析构函数的静态调用也一定会被内联展开。 * @author 光城 @@ -8,37 +8,28 @@ * @date 2019-07-24 */ -#include -using namespace std; -class Base -{ - public: - virtual void who() - { - cout << "I am Base\n"; - } -}; -class Derived: public Base -{ - public: - void who() - { - cout << "I am Derived\n"; - } -}; +#include +using namespace std; +class Base { +public: + virtual void who() { cout << "I am Base\n"; } +}; +class Derived : public Base { +public: + void who() { cout << "I am Derived\n"; } +}; -int main() -{ - // note here virtual function who() is called through - // object of the class (it will be resolved at compile - // time) so it can be inlined. - Base b; - b.who(); +int main() { + // note here virtual function who() is called through + // object of the class (it will be resolved at compile + // time) so it can be inlined. + Base b; + b.who(); - // Here virtual function is called through pointer, - // so it cannot be inlined - Base *ptr = new Derived(); - ptr->who(); + // Here virtual function is called through pointer, + // so it cannot be inlined + Base *ptr = new Derived(); + ptr->who(); - return 0; -} + return 0; +} diff --git a/basic_content/virtual/set4/BUILD b/basic_content/virtual/set4/BUILD new file mode 100644 index 00000000..c3d29f50 --- /dev/null +++ b/basic_content/virtual/set4/BUILD @@ -0,0 +1,8 @@ +# please run `bazel run basic_content/virtual/set4:rtti` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "rtti", + srcs = ["rtti.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/virtual/set4/rtti b/basic_content/virtual/set4/rtti deleted file mode 100755 index c7cb8919..00000000 Binary files a/basic_content/virtual/set4/rtti and /dev/null differ diff --git a/basic_content/virtual/set4/rtti.cpp b/basic_content/virtual/set4/rtti.cpp index 649687e3..f4198038 100644 --- a/basic_content/virtual/set4/rtti.cpp +++ b/basic_content/virtual/set4/rtti.cpp @@ -1,34 +1,38 @@ /** * @file rtti.cpp - * @brief 在面向对象程序设计中,有时我们需要在运行时查询一个对象是否能作为某种多态类型使用。与Java的instanceof,以及C#的as、is运算符类似,C++提供了dynamic_cast函数用于动态转型。相比C风格的强制类型转换和C++ reinterpret_cast,dynamic_cast提供了类型安全检查,是一种基于能力查询(Capability Query)的转换,所以在多态类型间进行转换更提倡采用dynamic_cast + * @brief + * 在面向对象程序设计中,有时我们需要在运行时查询一个对象是否能作为某种多态类型使用。与Java的instanceof,以及C#的as、is运算符类似,C++提供了dynamic_cast函数用于动态转型。相比C风格的强制类型转换和C++ + * reinterpret_cast,dynamic_cast提供了类型安全检查,是一种基于能力查询(Capability + * Query)的转换,所以在多态类型间进行转换更提倡采用dynamic_cast * @author 光城 * @version v1 * @date 2019-07-24 */ -// CPP program to illustrate -// // Run Time Type Identification -#include -#include -using namespace std; -class B { virtual void fun() {} }; -class D: public B { }; +// CPP program to illustrate +// Run Time Type Identification +#include +#include +using namespace std; +class B { + virtual void fun() {} +}; +class D : public B {}; -int main() -{ - B *b = new D; // 向上转型 - B &obj = *b; - D *d = dynamic_cast(b); // 向下转型 - if(d != NULL) - cout << "works"<(obj); - cout << "works"<(b); // 向下转型 + if (d != NULL) + cout << "works" << endl; + else + cout << "cannot cast B* to D*"; + + try { + D &dobj = dynamic_cast(obj); + cout << "works" << endl; + } catch (bad_cast bc) { // ERROR + cout << bc.what() << endl; + } + return 0; } diff --git a/basic_content/virtual/set4/warn_rtti.cpp b/basic_content/virtual/set4/warn_rtti.cpp index 986879db..4e027abc 100644 --- a/basic_content/virtual/set4/warn_rtti.cpp +++ b/basic_content/virtual/set4/warn_rtti.cpp @@ -1,35 +1,25 @@ // 在使用时需要注意:被转换对象obj的类型T1必须是多态类型,即T1必须公有继承自其它类,或者T1拥有虚函数(继承或自定义)。若T1为非多态类型,使用dynamic_cast会报编译错误。 -// A为非多态类型 +// A为非多态类型 -class A{ +class A {}; -}; - -//B为多态类型 - -class B{ - - public: virtual ~B(){} - -}; - -//D为非多态类型 +// B为多态类型 -class D: public A{ +class B { +public: + virtual ~B() {} }; -//E为非多态类型 - -class E : private A{ - -}; +// D为非多态类型 -//F为多态类型 +class D : public A {}; -class F : private B{ +// E为非多态类型 -} +class E : private A {}; +// F为多态类型 +class F : private B {} diff --git a/basic_content/volatile/BUILD b/basic_content/volatile/BUILD new file mode 100644 index 00000000..4aa0ef6f --- /dev/null +++ b/basic_content/volatile/BUILD @@ -0,0 +1,15 @@ +# please run `bazel run basic_content/volatile:noopt_vola` +# please run `bazel run basic_content/volatile:volatile` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "noopt_vola", + srcs = ["noopt_vola.cpp"], + copts = ["-std=c++11"] +) + +cc_binary( + name = "volatile", + srcs = ["volatile.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/volatile/README.md b/basic_content/volatile/README.md index 06cf454e..25221cd8 100644 --- a/basic_content/volatile/README.md +++ b/basic_content/volatile/README.md @@ -80,7 +80,7 @@ void threadFunc2() 可以。一个例子是只读的状态寄存器。它是volatile因为它可能被意想不到地改变。它是const因为程序不应该试图去修改它。 (2)一个指针可以是volatile吗?为什么? -可以。尽管这并不常见。一个例子是当一个中断服务子程序修该一个指向一个buffer的指针时。 +可以。尽管这并不常见。一个例子是当一个中断服务子程序修改一个指向一个buffer的指针时。 (3)下面的函数有什么错误? ```c++ diff --git a/basic_content/volatile/noopt_vola.cpp b/basic_content/volatile/noopt_vola.cpp index 58ceae6f..ebd860a6 100644 --- a/basic_content/volatile/noopt_vola.cpp +++ b/basic_content/volatile/noopt_vola.cpp @@ -1,15 +1,14 @@ /* Compile code without optimization option */ -#include -int main(void) -{ - const int local = 10; - int *ptr = (int*) &local; +#include +int main(void) { + const int local = 10; + int *ptr = (int *)&local; - printf("Initial value of local : %d \n", local); + printf("Initial value of local : %d \n", local); - *ptr = 100; + *ptr = 100; - printf("Modified value of local: %d \n", local); + printf("Modified value of local: %d \n", local); - return 0; -} + return 0; +} diff --git a/basic_content/volatile/nv b/basic_content/volatile/nv deleted file mode 100755 index b5eb220e..00000000 Binary files a/basic_content/volatile/nv and /dev/null differ diff --git a/basic_content/volatile/volatile.cpp b/basic_content/volatile/volatile.cpp index fcf6115a..4215abd5 100644 --- a/basic_content/volatile/volatile.cpp +++ b/basic_content/volatile/volatile.cpp @@ -1,16 +1,15 @@ /* Compile code with optimization option */ -#include +#include -int main(void) -{ - const volatile int local = 10; - int *ptr = (int*) &local; +int main(void) { + const volatile int local = 10; + int *ptr = (int *)&local; - printf("Initial value of local : %d \n", local); + printf("Initial value of local : %d \n", local); - *ptr = 100; + *ptr = 100; - printf("Modified value of local: %d \n", local); + printf("Modified value of local: %d \n", local); - return 0; -} + return 0; +} diff --git a/basic_content/vptr_vtable/BUILD b/basic_content/vptr_vtable/BUILD new file mode 100644 index 00000000..60ebb930 --- /dev/null +++ b/basic_content/vptr_vtable/BUILD @@ -0,0 +1,8 @@ +# please run `bazel run basic_content/vptr_vtable:vptr1` +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "vptr1", + srcs = ["vptr1.cpp"], + copts = ["-std=c++11"] +) \ No newline at end of file diff --git a/basic_content/vptr_vtable/vptr1.cpp b/basic_content/vptr_vtable/vptr1.cpp index a8f339ef..076bde7b 100644 --- a/basic_content/vptr_vtable/vptr1.cpp +++ b/basic_content/vptr_vtable/vptr1.cpp @@ -1,12 +1,3 @@ -/** - * @file vptr1.cpp - * @brief C++虚函数vptr和vtable - * 编译:g++ -g -o vptr vptr1.cpp -std=c++11 - * @author 光城 - * @version v1 - * @date 2019-07-20 - */ - #include #include using namespace std; @@ -16,90 +7,75 @@ using namespace std; */ typedef void (*Fun)(); - /** * @brief 基类 */ -class Base -{ - public: - Base(){}; - virtual void fun1() - { - cout << "Base::fun1()" << endl; - } - virtual void fun2() - { - cout << "Base::fun2()" << endl; - } - virtual void fun3(){} - ~Base(){}; +class Base { +public: + Base(){}; + virtual void fun1() { cout << "Base::fun1()" << endl; } + virtual void fun2() { cout << "Base::fun2()" << endl; } + virtual void fun3() {} + ~Base(){}; }; - /** * @brief 派生类 */ -class Derived: public Base -{ - public: - Derived(){}; - void fun1() - { - cout << "Derived::fun1()" << endl; - } - void fun2() - { - cout << "DerivedClass::fun2()" << endl; - } - ~Derived(){}; +class Derived : public Base { +public: + Derived(){}; + void fun1() { cout << "Derived::fun1()" << endl; } + void fun2() { cout << "DerivedClass::fun2()" << endl; } + ~Derived(){}; }; /** - * @brief 获取vptr地址与func地址,vptr指向的是一块内存,这块内存存放的是虚函数地址,这块内存就是我们所说的虚表 + * @brief + * 获取vptr地址与func地址,vptr指向的是一块内存,这块内存存放的是虚函数地址,这块内存就是我们所说的虚表 * * @param obj * @param offset * - * @return + * @return */ -Fun getAddr(void* obj,unsigned int offset) -{ - cout<<"======================="<fun1(); - cout<<"基类引用指向派生类实例并调用虚函数"<fun1(); + cout << "基类引用指向派生类实例并调用虚函数" << endl; + p.fun1(); - // 手动查找vptr 和 vtable - Fun f1 = getAddr(pt, 0); - (*f1)(); - Fun f2 = getAddr(pt, 1); - (*f2)(); + // 手动查找vptr 和 vtable + Fun f1 = getAddr(pt, 0); + (*f1)(); + Fun f2 = getAddr(pt, 1); + (*f2)(); - delete pt; - return 0; + delete pt; + return 0; } diff --git a/c++2.0/c++11/README.md b/c++2.0/c++11/README.md deleted file mode 100644 index 27b9e958..00000000 --- a/c++2.0/c++11/README.md +++ /dev/null @@ -1,70 +0,0 @@ -# C++新特性 - -## 1.[C++11新特性](./c++2.0/c++11) - -- [Variadic Templates](./c++2.0/variadic) - -- Spaces in Template Expressions - - ```cpp - vector > //ok in each C++ version - vector> // before c++ 11 error error: ‘>>’ should be ‘> >’ within a nested template argument list,c++11后可以正常通过 - ``` - -- [nullptr and nullptr_t](./c++2.0/nullptr.cpp) - -- [Automatic Type Deduction with auto](./c++2.0/auto.cpp) - -- [Uniform Initialization ](./c++2.0/uniform_initialization.cpp) - -- [initializer_list](./c++2.0/initializer.cpp) - -- [explicit for ctors taking more than one argument](./c++2.0/explicit.cpp) - -- [range-based for statement](./c++2.0/auto.cpp) - - ```cpp - for(decl:col) { - statement - } - ``` - -- [=default,=delete](./c++2.0/default_delete.cpp) - - 如果你自行定义了一个ctor,那么编译器就不会给你一个default ctor - 如果强制加上=default,就可以重新获得并使用default ctor. - -- Alias(化名)Template(template typedef) - - - [alias.cpp](./c++2.0/alias.cpp) - - [template_template.cpp](./c++2.0/template_template.cpp) - -- [template template parameter](./c++2.0/template_template.cpp) - -- [type alias](./c++2.0/type_alias.cpp) - -- [noexcept](./c++2.0/noexcept.cpp) - -- [override](./c++2.0/override.cpp) - -- [final](./c++2.0/final.cpp) - -- [decltype](./c++2.0/decltype.cpp) - -- [lambda](./c++2.0/lambda.cpp) - -- [Rvalue reference](./c++2.0/rvalue.cpp) - -- [move aware class](./c++2.0/move.cpp) - -- 容器-结构与分类 - - - (1) 序列式容器包括:array(C++2.0新引入),vector,deque,list,forward_list(C++2.0新引入) - - (2) 关联式容器包括:set/multiset,map/multimap - - (3) 无序容器(C++2.0新引入,更换原先hash_xxx为unordered_xxx)包括:unordered_map/unordered_multimap,unordered_set/unordered_multiset - -- [Hash Function](./c++2.0/hash.cpp) - -- [tuple](./c++2.0/tuple.cpp) - - 学习资料:https://www.bilibili.com/video/av51863195?from=search&seid=3610634846288253061 \ No newline at end of file diff --git "a/codingStyleIdioms/1_classInitializers/1.1_\347\261\273\344\271\213\351\227\264\345\265\214\345\245\227.cpp" b/codingStyleIdioms/1_classInitializers/1.1_nest.cpp similarity index 97% rename from "codingStyleIdioms/1_classInitializers/1.1_\347\261\273\344\271\213\351\227\264\345\265\214\345\245\227.cpp" rename to codingStyleIdioms/1_classInitializers/1.1_nest.cpp index 2693939c..aa3f5f58 100644 --- "a/codingStyleIdioms/1_classInitializers/1.1_\347\261\273\344\271\213\351\227\264\345\265\214\345\245\227.cpp" +++ b/codingStyleIdioms/1_classInitializers/1.1_nest.cpp @@ -1,3 +1,4 @@ +/* 1.1_类之间嵌套.cpp */ // // Created by light on 19-12-9. // diff --git "a/codingStyleIdioms/1_classInitializers/1.2_\346\227\240\351\273\230\350\256\244\346\236\204\351\200\240\347\232\204\347\273\247\346\211\277.cpp" b/codingStyleIdioms/1_classInitializers/1.2_nodefault_ctor.cpp similarity index 95% rename from "codingStyleIdioms/1_classInitializers/1.2_\346\227\240\351\273\230\350\256\244\346\236\204\351\200\240\347\232\204\347\273\247\346\211\277.cpp" rename to codingStyleIdioms/1_classInitializers/1.2_nodefault_ctor.cpp index 15e27bf8..035eaeef 100644 --- "a/codingStyleIdioms/1_classInitializers/1.2_\346\227\240\351\273\230\350\256\244\346\236\204\351\200\240\347\232\204\347\273\247\346\211\277.cpp" +++ b/codingStyleIdioms/1_classInitializers/1.2_nodefault_ctor.cpp @@ -1,3 +1,4 @@ +/* 1.2_无默认构造的继承.cpp */ // // Created by light on 19-12-9. // diff --git "a/codingStyleIdioms/1_classInitializers/1.3_\347\261\273\344\270\255const\346\225\260\346\215\256\346\210\220\345\221\230\343\200\201\345\274\225\347\224\250\346\225\260\346\215\256\346\210\220\345\221\230.cpp" b/codingStyleIdioms/1_classInitializers/1.3_const.cpp similarity index 84% rename from "codingStyleIdioms/1_classInitializers/1.3_\347\261\273\344\270\255const\346\225\260\346\215\256\346\210\220\345\221\230\343\200\201\345\274\225\347\224\250\346\225\260\346\215\256\346\210\220\345\221\230.cpp" rename to codingStyleIdioms/1_classInitializers/1.3_const.cpp index 5e221c45..c2b13861 100644 --- "a/codingStyleIdioms/1_classInitializers/1.3_\347\261\273\344\270\255const\346\225\260\346\215\256\346\210\220\345\221\230\343\200\201\345\274\225\347\224\250\346\225\260\346\215\256\346\210\220\345\221\230.cpp" +++ b/codingStyleIdioms/1_classInitializers/1.3_const.cpp @@ -1,3 +1,4 @@ +/* 1.3_类中const数据成员、引用数据成员.cpp */ // // Created by light on 19-12-9. // diff --git a/codingStyleIdioms/1_classInitializers/BUILD b/codingStyleIdioms/1_classInitializers/BUILD new file mode 100644 index 00000000..a1850adf --- /dev/null +++ b/codingStyleIdioms/1_classInitializers/BUILD @@ -0,0 +1,27 @@ +# please run `bazel run //codingStyleIdioms/1_classInitializers:1.2_nodefault_ctor` +# please run `bazel run //codingStyleIdioms/1_classInitializers:initializer` +# please run `bazel run //codingStyleIdioms/1_classInitializers:1.1_nest` +# please run `bazel run //codingStyleIdioms/1_classInitializers:1.3_const` + +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "1.2_nodefault_ctor", + srcs = ["1.2_nodefault_ctor.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "initializer", + srcs = ["initializer.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "1.1_nest", + srcs = ["1.1_nest.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "1.3_const", + srcs = ["1.3_const.cpp"], + copts = ["-std=c++11"], +) diff --git a/codingStyleIdioms/1_classInitializers/initializer.cpp b/codingStyleIdioms/1_classInitializers/initializer.cpp index 8bd8bfc8..3bef4526 100644 --- a/codingStyleIdioms/1_classInitializers/initializer.cpp +++ b/codingStyleIdioms/1_classInitializers/initializer.cpp @@ -1,3 +1,4 @@ +/* initializer.cpp */ // // Created by light on 19-12-9. // diff --git a/codingStyleIdioms/2_enumclass/BUILD b/codingStyleIdioms/2_enumclass/BUILD new file mode 100644 index 00000000..633721a1 --- /dev/null +++ b/codingStyleIdioms/2_enumclass/BUILD @@ -0,0 +1,9 @@ +# please run `bazel run //codingStyleIdioms/2_enumclass:namespace` + +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "namespace", + srcs = ["namespace.cpp"], + copts = ["-std=c++11"], +) diff --git a/codingStyleIdioms/3_RAII/BUILD b/codingStyleIdioms/3_RAII/BUILD new file mode 100644 index 00000000..70012bf9 --- /dev/null +++ b/codingStyleIdioms/3_RAII/BUILD @@ -0,0 +1,39 @@ +# please run `bazel run //codingStyleIdioms/3_RAII:RAII_fstram` +# please run `bazel run //codingStyleIdioms/3_RAII:c++_example1` +# please run `bazel run //codingStyleIdioms/3_RAII:c++_example` +# please run `bazel run //codingStyleIdioms/3_RAII:RAII` +# please run `bazel run //codingStyleIdioms/3_RAII:c_example` +# please run `bazel run //codingStyleIdioms/3_RAII:c++_example2` + +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "RAII_fstram", + srcs = ["RAII_fstram.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "c++_example1", + srcs = ["c++_example1.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "c++_example", + srcs = ["c++_example.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "RAII", + srcs = ["RAII.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "c_example", + srcs = ["c_example.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "c++_example2", + srcs = ["c++_example2.cpp"], + copts = ["-std=c++11"], +) diff --git a/codingStyleIdioms/4_copy-swap/BUILD b/codingStyleIdioms/4_copy-swap/BUILD new file mode 100644 index 00000000..85aa3ed8 --- /dev/null +++ b/codingStyleIdioms/4_copy-swap/BUILD @@ -0,0 +1,9 @@ +# please run `bazel run //codingStyleIdioms/4_copy-swap:copy-swapAndADL` + +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "copy-swapAndADL", + srcs = ["copy-swapAndADL.cpp"], + copts = ["-std=c++11"], +) diff --git a/codingStyleIdioms/5_pImpl/.a.cpp.un~ b/codingStyleIdioms/5_pImpl/.a.cpp.un~ deleted file mode 100644 index 16b83e6d..00000000 Binary files a/codingStyleIdioms/5_pImpl/.a.cpp.un~ and /dev/null differ diff --git a/codingStyleIdioms/5_pImpl/.pimplTime.cpp.un~ b/codingStyleIdioms/5_pImpl/.pimplTime.cpp.un~ deleted file mode 100644 index 76271a23..00000000 Binary files a/codingStyleIdioms/5_pImpl/.pimplTime.cpp.un~ and /dev/null differ diff --git a/codingStyleIdioms/5_pImpl/.timePimpl.cpp.un~ b/codingStyleIdioms/5_pImpl/.timePimpl.cpp.un~ deleted file mode 100644 index 488ccca0..00000000 Binary files a/codingStyleIdioms/5_pImpl/.timePimpl.cpp.un~ and /dev/null differ diff --git a/codingStyleIdioms/5_pImpl/BUILD b/codingStyleIdioms/5_pImpl/BUILD new file mode 100644 index 00000000..480434cd --- /dev/null +++ b/codingStyleIdioms/5_pImpl/BUILD @@ -0,0 +1,21 @@ +# please run `bazel run //codingStyleIdioms/5_pImpl:pimpl` +# please run `bazel run //codingStyleIdioms/5_pImpl:pimplTime` +# please run `bazel run //codingStyleIdioms/5_pImpl:noPimpl` + +load("@rules_cc//cc:defs.bzl", "cc_binary") + +cc_binary( + name = "pimpl", + srcs = ["pimpl.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "pimplTime", + srcs = ["pimplTime.cpp"], + copts = ["-std=c++11"], +) +cc_binary( + name = "noPimpl", + srcs = ["noPimpl.cpp"], + copts = ["-std=c++11"], +) diff --git a/codingStyleIdioms/5_pImpl/a.cpp~ b/codingStyleIdioms/5_pImpl/a.cpp~ deleted file mode 100644 index 7a865b07..00000000 --- a/codingStyleIdioms/5_pImpl/a.cpp~ +++ /dev/null @@ -1,24 +0,0 @@ -#include -using namespace std; - -class C { - -public: - virtual void print(); -}; - - -class CC:public C { -public: - void print() { - cout<<"CC"<(new C); - //c->print(); -} - - diff --git a/codingStyleIdioms/5_pImpl/pimplTime.cpp~ b/codingStyleIdioms/5_pImpl/pimplTime.cpp~ deleted file mode 100644 index 7fc3dd2d..00000000 --- a/codingStyleIdioms/5_pImpl/pimplTime.cpp~ +++ /dev/null @@ -1,11 +0,0 @@ -#include "pimplTime.h" - -struct X::XImpl { - C c; - D d; -} - -int main() { - X x; - -} diff --git a/codingStyleIdioms/5_pImpl/timePimpl.cpp~ b/codingStyleIdioms/5_pImpl/timePimpl.cpp~ deleted file mode 100644 index 41450d51..00000000 --- a/codingStyleIdioms/5_pImpl/timePimpl.cpp~ +++ /dev/null @@ -1 +0,0 @@ -#include <> diff --git a/concurrency/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml b/concurrency/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml deleted file mode 100644 index f08604bb..00000000 --- a/concurrency/Threading_In_CPlusPlus/.idea/Threading_In_CPlusPlus.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/concurrency/Threading_In_CPlusPlus/.idea/misc.xml b/concurrency/Threading_In_CPlusPlus/.idea/misc.xml deleted file mode 100644 index 8822db8f..00000000 --- a/concurrency/Threading_In_CPlusPlus/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/concurrency/Threading_In_CPlusPlus/.idea/modules.xml b/concurrency/Threading_In_CPlusPlus/.idea/modules.xml deleted file mode 100644 index 93828d7a..00000000 --- a/concurrency/Threading_In_CPlusPlus/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/concurrency/Threading_In_CPlusPlus/.idea/workspace.xml b/concurrency/Threading_In_CPlusPlus/.idea/workspace.xml deleted file mode 100644 index 648a7014..00000000 --- a/concurrency/Threading_In_CPlusPlus/.idea/workspace.xml +++ /dev/null @@ -1,510 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -