support bazel complie this project and format code.
This commit is contained in:
parent
1f86192576
commit
7529ae3a55
6
.gitignore
vendored
6
.gitignore
vendored
@ -1 +1,7 @@
|
||||
com.sh
|
||||
bazel-CPlusPlusThings
|
||||
bazel-bin
|
||||
bazel-out
|
||||
bazel-testlogs
|
||||
bazel-cplusplus_bazel
|
||||
.vscode
|
112
.vscode/settings.json
vendored
Normal file
112
.vscode/settings.json
vendored
Normal file
@ -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"
|
||||
}
|
||||
}
|
106
README.md
106
README.md
@ -155,15 +155,15 @@
|
||||
├── [输入输出重载.cpp](./practical_exercises/key_exercises/输入输出重载.cpp)
|
||||
├── [输出格式.cpp](./practical_exercises/key_exercises/输出格式.cpp)
|
||||
|
||||
### 3.[C++2.0 新特性](./c++2.0)
|
||||
### 3.[C++2.0 新特性](./cpp2.0)
|
||||
|
||||
#### 3.0 概况
|
||||
|
||||
C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。
|
||||
|
||||
#### 3.1 [C++11 新特性](./c++2.0/c++11)
|
||||
#### 3.1 [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 +172,17 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。
|
||||
vector<list<int>> // 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 +190,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,9 +224,9 @@ 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
|
||||
|
||||
@ -295,31 +295,31 @@ C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。
|
||||
|
||||
#### 8.1 [极客时间《现代 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)
|
||||
- [栈](./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)
|
||||
|
||||
### 9.工具篇
|
||||
|
||||
@ -376,20 +376,20 @@ map<int, int> mp{
|
||||
|
||||
|
||||
|
||||
### 12.赞助我
|
||||
### 12.关注我
|
||||
|
||||
如果觉得不错,赞助我吧~
|
||||
如果觉得不错,关注我吧~
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th align="center" style="height=200 width="200">
|
||||
<img src="https://light-city.club/sc/assets/images/alipay.jpg" height="200" width="200" ><br>
|
||||
支付宝
|
||||
<img src="./img/cpp.jpg" height="200" width="200" ><br>
|
||||
星球
|
||||
</th>
|
||||
<th align="center" style="height=200 width="200">
|
||||
<img src="https://light-city.club/sc/assets/images/wechat.png" height="200" width="200" ><br>
|
||||
微信
|
||||
<img src="./img/wechat.jpg" height="200" width="200" ><br>
|
||||
微信公众号
|
||||
</th>
|
||||
</tr>
|
||||
</tbody>
|
||||
|
48
README_EN.md
48
README_EN.md
@ -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:Understangding the complexity of concurrency](./learn_class/modern_cpp_30/memorymodel_atomic)
|
||||
|
||||
### 7.Tools
|
||||
|
||||
|
43
basic_content/abstract/BUILD
Normal file
43
basic_content/abstract/BUILD
Normal file
@ -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"],
|
||||
)
|
@ -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.实现抽象类
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file abstract.cpp
|
||||
* @brief 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数
|
||||
* @brief
|
||||
* 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数
|
||||
* 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类
|
||||
* @author 光城
|
||||
* @version v1
|
||||
|
21
basic_content/abstract/abstract_base.h
Normal file
21
basic_content/abstract/abstract_base.h
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @file abstreact_base.cpp
|
||||
* @brief
|
||||
* C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数!
|
||||
* 纯虚函数:没有函数体的虚函数
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-07-20
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief 抽象类
|
||||
*/
|
||||
class AbstractBase {
|
||||
// Data members of class
|
||||
public:
|
||||
// Pure Virtual Function
|
||||
virtual void show() = 0;
|
||||
|
||||
/* Other members */
|
||||
};
|
@ -9,23 +9,20 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Base
|
||||
{
|
||||
class Base {
|
||||
int x;
|
||||
|
||||
public:
|
||||
virtual void fun() = 0;
|
||||
int getX() { return x; }
|
||||
};
|
||||
|
||||
class Derived: public Base
|
||||
{
|
||||
int y;
|
||||
class Derived : public Base {
|
||||
public:
|
||||
void fun() { cout << "fun() called"; } // 实现了fun()函数
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
Derived d;
|
||||
d.fun();
|
||||
return 0;
|
||||
|
@ -12,17 +12,15 @@ using namespace std;
|
||||
/**
|
||||
* @brief 抽象类至少包含一个纯虚函数
|
||||
*/
|
||||
class Test
|
||||
{
|
||||
class Test {
|
||||
int x;
|
||||
|
||||
public:
|
||||
virtual void show() = 0;
|
||||
int getX() { return x; }
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
Test t; // error! 不能创建抽象类的对象
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -9,30 +9,25 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
/**
|
||||
* @brief 抽象类至少包含一个纯虚函数
|
||||
*/
|
||||
class Base
|
||||
{
|
||||
class Base {
|
||||
int x;
|
||||
|
||||
public:
|
||||
virtual void show() = 0;
|
||||
int getX() { return x; }
|
||||
|
||||
};
|
||||
class Derived: public Base
|
||||
{
|
||||
class Derived : public Base {
|
||||
public:
|
||||
void show() { cout << "In Derived \n"; }
|
||||
Derived() {}
|
||||
};
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
// Base b; //error! 不能创建抽象类的对象
|
||||
// Base *b = new Base(); error!
|
||||
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
|
||||
bp->show();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -9,21 +9,20 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Base
|
||||
{
|
||||
class Base {
|
||||
int x;
|
||||
|
||||
public:
|
||||
virtual void show() = 0;
|
||||
int getX() { return x; }
|
||||
};
|
||||
class Derived: public Base
|
||||
{
|
||||
class Derived : public Base {
|
||||
public:
|
||||
// void show() { }
|
||||
};
|
||||
int main(void)
|
||||
{
|
||||
Derived d; //error! 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
|
||||
int main(void) {
|
||||
Derived
|
||||
d; // error!
|
||||
// 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -10,25 +10,24 @@
|
||||
using namespace std;
|
||||
|
||||
// An abstract class with constructor
|
||||
class Base
|
||||
{
|
||||
class Base {
|
||||
protected:
|
||||
int x;
|
||||
|
||||
public:
|
||||
virtual void fun() = 0;
|
||||
Base(int i) { x = i; }
|
||||
};
|
||||
|
||||
class Derived: public Base
|
||||
{
|
||||
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)
|
||||
{
|
||||
int main(void) {
|
||||
Derived d(4, 5);
|
||||
d.fun();
|
||||
return 0;
|
||||
|
@ -27,4 +27,3 @@ int main() {
|
||||
delete Var;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -11,17 +11,15 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
class A
|
||||
{
|
||||
class A {
|
||||
private:
|
||||
int a;
|
||||
|
||||
public:
|
||||
virtual void show() = 0; // 纯虚函数
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
/*
|
||||
* 1. 抽象类只能作为基类来派生新类使用
|
||||
* 2. 抽象类的指针和引用->由抽象类派生出来的类的对象!
|
||||
|
@ -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 */
|
||||
};
|
13
basic_content/assert/BUILD
Normal file
13
basic_content/assert/BUILD
Normal file
@ -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"],
|
||||
)
|
@ -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
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include<assert.h>
|
||||
|
||||
int main(){
|
||||
int x=7;
|
||||
assert(x==5);
|
||||
return 0;
|
||||
}
|
||||
|
7
basic_content/bit/BUILD
Normal file
7
basic_content/bit/BUILD
Normal file
@ -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"],
|
||||
)
|
@ -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)
|
||||
|
||||
## 位域的初始化和位的重映射
|
||||
|
||||
|
18
basic_content/bit/bit.cpp
Normal file
18
basic_content/bit/bit.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#include<iostream>
|
||||
|
||||
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;
|
||||
}
|
13
basic_content/c_poly/BUILD
Normal file
13
basic_content/c_poly/BUILD
Normal file
@ -0,0 +1,13 @@
|
||||
# please run `bazel run basic_content/c_poly:c_examp`
|
||||
# please run `bazel run basic_content/c_poly:c++_examp`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "c_examp",
|
||||
srcs = ["c_examp.c"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "c++_examp",
|
||||
srcs = ["c++_examp.cpp"],
|
||||
)
|
Binary file not shown.
@ -9,8 +9,7 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
class A
|
||||
{
|
||||
class A {
|
||||
public:
|
||||
virtual void f() //虚函数实现
|
||||
{
|
||||
@ -27,9 +26,7 @@ class B:public A // 必须为共有继承,否则后面调用不到,class
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
A a; //基类对象
|
||||
B b; //派生类对象
|
||||
|
||||
|
Binary file not shown.
17
basic_content/const/BUILD
Normal file
17
basic_content/const/BUILD
Normal file
@ -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"]
|
||||
)
|
@ -298,41 +298,42 @@ public:
|
||||
int getCount() const;
|
||||
|
||||
};
|
||||
//main.cpp
|
||||
#include<iostream>
|
||||
#include"apple.cpp"
|
||||
using namespace std;
|
||||
|
||||
// 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 "<<num<<endl;
|
||||
std::cout << "take func " << num << std::endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
add(); // error
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main(){
|
||||
int main()
|
||||
{
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
return 0;
|
||||
}
|
||||
// main.cpp
|
||||
```
|
||||
> 编译: g++ -o main main.cpp apple.cpp<br>
|
||||
> 编译:bazel run basic_content/const/class_const/first_example:main<br>
|
||||
|
||||
此时报错,上面getCount()方法中调用了一个add方法,而add方法并非const修饰,所以运行报错。也就是说const成员函数只能访问const成员函数。
|
||||
|
||||
|
16
basic_content/const/class_const/c++11_example/BUILD
Normal file
16
basic_content/const/class_const/c++11_example/BUILD
Normal file
@ -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",
|
||||
],
|
||||
)
|
@ -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 <iostream>
|
||||
|
||||
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;
|
||||
}
|
13
basic_content/const/class_const/c++11_example/apple.h
Normal file
13
basic_content/const/class_const/c++11_example/apple.h
Normal file
@ -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;
|
||||
};
|
Binary file not shown.
@ -1,26 +1,6 @@
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
#include"apple.cpp"
|
||||
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 "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
|
16
basic_content/const/class_const/first_example/BUILD
Normal file
16
basic_content/const/class_const/first_example/BUILD
Normal file
@ -0,0 +1,16 @@
|
||||
# please run `bazel run basic_content/const/class_const/first_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",
|
||||
],
|
||||
)
|
@ -1,13 +1,20 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
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;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
11
basic_content/const/class_const/first_example/apple.h
Normal file
11
basic_content/const/class_const/first_example/apple.h
Normal file
@ -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;
|
||||
};
|
@ -1,27 +1,7 @@
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
#include"apple.cpp"
|
||||
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 "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
|
18
basic_content/const/class_const/overload_example/BUILD
Normal file
18
basic_content/const/class_const/overload_example/BUILD
Normal file
@ -0,0 +1,18 @@
|
||||
# please run `bazel run basic_content/const/class_const/overload_example:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "apple",
|
||||
srcs = ["apple.cpp"],
|
||||
hdrs = ["apple.h"],
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
deps = [
|
||||
":apple",
|
||||
],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@ -1,13 +1,22 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
public:
|
||||
Apple(int i);
|
||||
static const int apple_number;
|
||||
void take(int num) const;
|
||||
int add();
|
||||
int add(int num) const;
|
||||
int getCount() const;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
11
basic_content/const/class_const/overload_example/apple.h
Normal file
11
basic_content/const/class_const/overload_example/apple.h
Normal file
@ -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;
|
||||
};
|
Binary file not shown.
@ -1,33 +1,7 @@
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
#include"apple.cpp"
|
||||
using namespace std;
|
||||
const int Apple::apple_number=10;
|
||||
#include<iostream>
|
||||
#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 "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
add(); // error
|
||||
return apple_number;
|
||||
}
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout << a.getCount() << endl;
|
||||
|
17
basic_content/const/class_const/static_example/BUILD
Normal file
17
basic_content/const/class_const/static_example/BUILD
Normal file
@ -0,0 +1,17 @@
|
||||
# please run `bazel run basic_content/const/class_const/static_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",
|
||||
],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@ -1,14 +1,22 @@
|
||||
class Apple
|
||||
{
|
||||
private:
|
||||
int people[100];
|
||||
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;
|
||||
};
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
11
basic_content/const/class_const/static_example/apple.h
Normal file
11
basic_content/const/class_const/static_example/apple.h
Normal file
@ -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;
|
||||
};
|
Binary file not shown.
@ -1,32 +1,9 @@
|
||||
#include "apple.h"
|
||||
#include <iostream>
|
||||
#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 "<<num<<endl;
|
||||
}
|
||||
int Apple::getCount() const
|
||||
{
|
||||
take(1);
|
||||
// add(); //error
|
||||
return apple_number;
|
||||
}
|
||||
int main() {
|
||||
Apple a(2);
|
||||
cout<<a.getCount()<<endl;
|
||||
cout<<a.ap<<endl;
|
||||
std::cout << a.getCount() << std::endl;
|
||||
std::cout << a.ap << std::endl;
|
||||
a.add(10);
|
||||
const Apple b(3);
|
||||
b.add(100);
|
||||
|
@ -6,6 +6,4 @@ void f(const int i){
|
||||
cout << i << endl;
|
||||
}
|
||||
|
||||
int main(){
|
||||
f(1);
|
||||
}
|
||||
int main() { f(1); }
|
||||
|
14
basic_content/const/extern_const/BUILD
Normal file
14
basic_content/const/extern_const/BUILD
Normal file
@ -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"]
|
||||
)
|
@ -5,7 +5,4 @@
|
||||
* execute: ./file
|
||||
*/
|
||||
extern const int ext;
|
||||
int main(){
|
||||
|
||||
std::cout<<ext<<std::endl;
|
||||
}
|
||||
int main() { std::cout << ext << std::endl; }
|
@ -5,7 +5,4 @@
|
||||
* execute: ./file
|
||||
*/
|
||||
extern int ext;
|
||||
int main(){
|
||||
|
||||
std::cout<<(ext+10)<<std::endl;
|
||||
}
|
||||
int main() { std::cout << (ext + 10) << std::endl; }
|
24
basic_content/const/funciton_const/condition1/BUILD
Normal file
24
basic_content/const/funciton_const/condition1/BUILD
Normal file
@ -0,0 +1,24 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition1`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition2`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition3`
|
||||
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 = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition2",
|
||||
srcs = ["condition2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "condition3",
|
||||
srcs = ["condition3.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main() {
|
||||
const int *ptr;
|
||||
*ptr = 10; // error
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main() {
|
||||
const int p = 10;
|
||||
const void *vp = &p;
|
||||
|
@ -8,5 +8,4 @@ int main(){
|
||||
int *ptr1 = &val;
|
||||
*ptr1 = 4;
|
||||
cout << *ptr << endl;
|
||||
|
||||
}
|
||||
|
23
basic_content/const/funciton_const/condition2/BUILD
Normal file
23
basic_content/const/funciton_const/condition2/BUILD
Normal file
@ -0,0 +1,23 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition1`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition2`
|
||||
# please run `bazel run basic_content/const/funciton_const/condition2:condition3`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "condition2",
|
||||
srcs = ["condition2.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "condition3",
|
||||
srcs = ["condition3.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
|
||||
int num = 0;
|
||||
int *const ptr = # // const指针必须初始化!且const指针的值不能修改
|
||||
int *t = #
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
|
||||
const int num = 0;
|
||||
int *const ptr = # // error! const int* -> int*
|
||||
cout << *ptr << endl;
|
||||
|
Binary file not shown.
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main() {
|
||||
|
||||
const int num = 10;
|
||||
const int *const ptr = # // error! const int* -> int*
|
||||
cout << *ptr << endl;
|
||||
|
9
basic_content/const/funciton_const/condition3/BUILD
Normal file
9
basic_content/const/funciton_const/condition3/BUILD
Normal file
@ -0,0 +1,9 @@
|
||||
# please run `bazel run basic_content/const/funciton_const/condition3:condition1`
|
||||
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 = "condition1",
|
||||
srcs = ["condition1.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@ -2,9 +2,7 @@
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
const int p = 3;
|
||||
const int *const ptr = &p;
|
||||
cout << *ptr << endl;
|
||||
|
||||
}
|
||||
|
8
basic_content/decltype/BUILD
Normal file
8
basic_content/decltype/BUILD
Normal file
@ -0,0 +1,8 @@
|
||||
# please run `bazel run basic_content/decltype:decltype`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "decltype",
|
||||
srcs = ["decltype.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@ -1,11 +1,3 @@
|
||||
/**
|
||||
* @file decltype.cpp
|
||||
* @brief g++ -o decltype decltype.cpp -std=c++11
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-08
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
@ -14,13 +6,11 @@ using namespace std;
|
||||
*/
|
||||
template <typename T>
|
||||
|
||||
auto multiply(T x, T y)->decltype(x*y)
|
||||
{
|
||||
auto multiply(T x, T y) -> decltype(x * y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int nums[] = {1, 2, 3, 4};
|
||||
vector<int> vec(nums, nums + 4);
|
||||
vector<int>::iterator it;
|
||||
@ -29,14 +19,12 @@ int main()
|
||||
cout << *it << " ";
|
||||
cout << endl;
|
||||
|
||||
|
||||
using nullptr_t = decltype(nullptr);
|
||||
nullptr_t nu;
|
||||
int *p = NULL;
|
||||
if (p == nu)
|
||||
cout << "NULL" << endl;
|
||||
|
||||
|
||||
typedef decltype(vec.begin()) vectype;
|
||||
|
||||
for (vectype i = vec.begin(); i != vec.end(); i++)
|
||||
@ -46,15 +34,14 @@ int main()
|
||||
/**
|
||||
* 匿名结构体
|
||||
*/
|
||||
struct
|
||||
{
|
||||
struct {
|
||||
int d;
|
||||
double b;
|
||||
} anon_s;
|
||||
|
||||
decltype(anon_s) as; // 定义了一个上面匿名的结构体
|
||||
decltype(anon_s) as{1, 2.0}; // 定义了一个上面匿名的结构体
|
||||
|
||||
cout<<multiply(11,2)<<endl;
|
||||
cout << multiply(11, 2) << ":" << as.b << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
16
basic_content/enum/BUILD
Normal file
16
basic_content/enum/BUILD
Normal file
@ -0,0 +1,16 @@
|
||||
# please run `bazel run basic_content/enum:classic_practice`
|
||||
# please run `bazel run basic_content/enum:tradition_color`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "classic_practice",
|
||||
srcs = ["classic_practice.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
|
||||
cc_binary(
|
||||
name = "tradition_color",
|
||||
srcs = ["tradition_color.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@ -15,14 +15,22 @@
|
||||
- 作用域不受限,会容易引起命名冲突。例如下面无法编译通过的:
|
||||
|
||||
```c++
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
enum Color {RED,BLUE};
|
||||
enum Feeling {EXCITED,BLUE};
|
||||
enum Color
|
||||
{
|
||||
RED,
|
||||
BLUE
|
||||
};
|
||||
enum Feeling
|
||||
{
|
||||
EXCITED,
|
||||
BLUE
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Color a = BLUE; // error
|
||||
Feeling b = EXCITED;
|
||||
std::cout << a << ":" << b << std::endl;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
Binary file not shown.
@ -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 <iostream>
|
||||
using namespace std;
|
||||
/**
|
||||
* @brief namespace解决作用域不受限
|
||||
*/
|
||||
namespace Color
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
RED=15,
|
||||
YELLOW,
|
||||
BLUE
|
||||
};
|
||||
namespace Color {
|
||||
enum Type { RED = 15, YELLOW, BLUE };
|
||||
};
|
||||
|
||||
/**
|
||||
@ -32,38 +18,22 @@ 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
|
||||
{
|
||||
RED='r',
|
||||
BLUE
|
||||
};
|
||||
enum class Color3 : char { RED = 'r', BLUE };
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// 定义新的枚举变量
|
||||
Color::Type c = Color::RED;
|
||||
cout << c << endl;
|
||||
|
@ -4,7 +4,9 @@ using namespace std;
|
||||
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;
|
||||
}
|
||||
|
9
basic_content/explicit/BUILD
Normal file
9
basic_content/explicit/BUILD
Normal file
@ -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"]
|
||||
)
|
Binary file not shown.
@ -2,14 +2,12 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct A
|
||||
{
|
||||
struct A {
|
||||
A(int) {}
|
||||
operator bool() const { return true; }
|
||||
};
|
||||
|
||||
struct B
|
||||
{
|
||||
struct B {
|
||||
explicit B(int) {}
|
||||
explicit operator bool() const { return true; }
|
||||
};
|
||||
@ -18,28 +16,35 @@ void doA(A a) {}
|
||||
|
||||
void doB(B b) {}
|
||||
|
||||
int main()
|
||||
{
|
||||
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 的隐式转换
|
||||
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<bool>(a1); // OK :static_cast 进行直接初始化
|
||||
|
||||
B b1(1); // OK:直接初始化
|
||||
// B b2 = 1; // 错误:被 explicit 修饰构造函数的对象不可以复制初始化
|
||||
// B b2 = 1; // 错误:被 explicit
|
||||
// 修饰构造函数的对象不可以复制初始化
|
||||
B b3{1}; // OK:直接列表初始化
|
||||
// B b4 = { 1 }; // 错误:被 explicit 修饰构造函数的对象不可以复制列表初始化
|
||||
// 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() 的对象不可以隐式转换
|
||||
// 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<bool>(b1); // OK:static_cast 进行直接初始化
|
||||
|
||||
return 0;
|
||||
|
4
basic_content/extern/README.md
vendored
4
basic_content/extern/README.md
vendored
@ -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
|
||||
|
15
basic_content/extern/extern_c++/BUILD
vendored
Normal file
15
basic_content/extern/extern_c++/BUILD
vendored
Normal file
@ -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"]
|
||||
)
|
2
basic_content/extern/extern_c++/add.cpp
vendored
2
basic_content/extern/extern_c++/add.cpp
vendored
@ -4,6 +4,6 @@ extern "C" {
|
||||
#include "add.h"
|
||||
}
|
||||
int main() {
|
||||
add(2,3);
|
||||
std::cout << add(2, 3) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
BIN
basic_content/extern/extern_c++/add.o
vendored
BIN
basic_content/extern/extern_c++/add.o
vendored
Binary file not shown.
BIN
basic_content/extern/extern_c++/main
vendored
BIN
basic_content/extern/extern_c++/main
vendored
Binary file not shown.
14
basic_content/extern/extern_c/BUILD
vendored
Normal file
14
basic_content/extern/extern_c/BUILD
vendored
Normal file
@ -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"],
|
||||
)
|
4
basic_content/extern/extern_c/add.cpp
vendored
4
basic_content/extern/extern_c/add.cpp
vendored
@ -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; }
|
||||
|
BIN
basic_content/extern/extern_c/add.o
vendored
BIN
basic_content/extern/extern_c/add.o
vendored
Binary file not shown.
BIN
basic_content/extern/extern_c/main
vendored
BIN
basic_content/extern/extern_c/main
vendored
Binary file not shown.
15
basic_content/friend/BUILD
Normal file
15
basic_content/friend/BUILD
Normal file
@ -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"]
|
||||
)
|
Binary file not shown.
@ -2,25 +2,21 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
class A {
|
||||
public:
|
||||
A(int _a) : a(_a){};
|
||||
friend class B;
|
||||
|
||||
private:
|
||||
int a;
|
||||
};
|
||||
|
||||
class B
|
||||
{
|
||||
class B {
|
||||
public:
|
||||
int getb(A ca) {
|
||||
return ca.a;
|
||||
};
|
||||
int getb(A ca) { return ca.a; };
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
A a(3);
|
||||
B b;
|
||||
cout << b.getb(a) << endl;
|
||||
|
Binary file not shown.
@ -1,17 +1,8 @@
|
||||
/**
|
||||
* @file friend_func.cpp
|
||||
* @brief 友元函数
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-06
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
class A {
|
||||
public:
|
||||
A(int _a) : a(_a){};
|
||||
friend int geta(A &ca); ///< 友元函数
|
||||
@ -19,13 +10,9 @@ private:
|
||||
int a;
|
||||
};
|
||||
|
||||
int geta(A &ca)
|
||||
{
|
||||
return ca.a;
|
||||
}
|
||||
int geta(A &ca) { return ca.a; }
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
A a(3);
|
||||
cout << geta(a) << endl;
|
||||
|
||||
|
8
basic_content/func_pointer/BUILD
Normal file
8
basic_content/func_pointer/BUILD
Normal file
@ -0,0 +1,8 @@
|
||||
# please run `bazel run basic_content/func_pointer:func_pointer`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "func_pointer",
|
||||
srcs = ["func_pointer.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@ -10,7 +10,8 @@
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* @brief 定义了一个变量pFun,这个变量是个指针,指向返回值为空和参数为int的函数的指针!
|
||||
* @brief
|
||||
* 定义了一个变量pFun,这个变量是个指针,指向返回值为空和参数为int的函数的指针!
|
||||
*/
|
||||
void (*pFun)(int);
|
||||
|
||||
@ -19,10 +20,7 @@ void (*pFun)(int);
|
||||
*/
|
||||
typedef void (*func)(void);
|
||||
|
||||
void myfunc(void)
|
||||
{
|
||||
cout<<"asda"<<endl;
|
||||
}
|
||||
void myfunc(void) { cout << "asda" << endl; }
|
||||
|
||||
void glFun(int a) { cout << a << endl; }
|
||||
int main() {
|
||||
|
23
basic_content/inline/BUILD
Normal file
23
basic_content/inline/BUILD
Normal file
@ -0,0 +1,23 @@
|
||||
# please run `bazel run basic_content/inline:inline_virtual`
|
||||
# please run `bazel run basic_content/inline:main`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "inline",
|
||||
hdrs = ["inline.h"],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "inline_virtual",
|
||||
srcs = ["inline_virtual.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["inline.cpp"],
|
||||
deps = [
|
||||
":inline",
|
||||
],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
@ -27,7 +27,7 @@ public:
|
||||
{
|
||||
|
||||
};
|
||||
void f1(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。
|
||||
void f2(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。
|
||||
};
|
||||
```
|
||||
|
||||
@ -128,7 +128,7 @@ int main()
|
||||
delete ptr;
|
||||
ptr = nullptr;
|
||||
|
||||
system("pause");
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
Binary file not shown.
@ -1,12 +1,11 @@
|
||||
#include <iostream>
|
||||
#include "inline.h"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* @brief inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字”
|
||||
* @brief
|
||||
* inline要起作用,inline要与函数定义放在一起,inline是一种“用于实现的关键字,而不是用于声明的关键字”
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
@ -19,18 +18,12 @@ inline int Foo(int x,int y) // 函数定义
|
||||
return x + y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 定义处加inline关键字,推荐这种写法!
|
||||
inline void A::f1(int x){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
inline void A::f1(int x) {}
|
||||
|
||||
/**
|
||||
* @brief 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。
|
||||
* @brief
|
||||
* 内联能提高函数效率,但并不是所有的函数都定义成内联函数!内联是以代码膨胀(复制)为代价,仅仅省去了函数调用的开销,从而提高函数的执行效率。
|
||||
* 如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收货会更少!另一方面,每一处内联函数的调用都要复制代码,将使程序的总代码量增大,消耗更多的内存空间。
|
||||
* 以下情况不宜用内联:
|
||||
* (1) 如果函数体内的代码比较长,使得内联将导致内存消耗代价比较高。
|
||||
@ -38,20 +31,12 @@ inline void A::f1(int x){
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
|
||||
|
||||
cout<<Foo(1,2)<<endl;
|
||||
|
||||
}
|
||||
int main() { cout << Foo(1, 2) << endl; }
|
||||
/**
|
||||
* 编译器对 inline 函数的处理步骤
|
||||
* 将 inline 函数体复制到 inline 函数调用点处;
|
||||
* 为所用 inline 函数中的局部变量分配内存空间;
|
||||
* 将 inline 函数的的输入参数和返回值映射到调用方法的局部变量空间中;
|
||||
* 如果 inline 函数有多个返回点,将其转变为 inline 函数代码块末尾的分支(使用 GOTO)。
|
||||
* 如果 inline 函数有多个返回点,将其转变为 inline 函数代码块末尾的分支(使用
|
||||
* GOTO)。
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
|
||||
class A
|
||||
{
|
||||
#pragma once
|
||||
class A {
|
||||
public:
|
||||
void f1(int x);
|
||||
|
||||
/**
|
||||
* @brief 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。
|
||||
* @brief
|
||||
* 类中定义了的函数是隐式内联函数,声明要想成为内联函数,必须在实现处(定义处)加inline关键字。
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
@ -14,6 +14,5 @@ public:
|
||||
{
|
||||
|
||||
};
|
||||
void f1(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。
|
||||
|
||||
void f2(int x); ///< 声明后,要想成为内联函数,必须在定义处加inline关键字。
|
||||
};
|
||||
|
@ -1,16 +1,11 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base
|
||||
{
|
||||
class Base {
|
||||
public:
|
||||
inline virtual void who()
|
||||
{
|
||||
cout << "I am Base\n";
|
||||
}
|
||||
inline virtual void who() { cout << "I am Base\n"; }
|
||||
virtual ~Base() {}
|
||||
};
|
||||
class Derived : public Base
|
||||
{
|
||||
class Derived : public Base {
|
||||
public:
|
||||
inline void who() // 不写inline时隐式内联
|
||||
{
|
||||
@ -18,9 +13,9 @@ class Derived : public Base
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
// 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。
|
||||
int main() {
|
||||
// 此处的虚函数
|
||||
// who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。
|
||||
Base b;
|
||||
b.who();
|
||||
|
||||
@ -28,7 +23,8 @@ int main()
|
||||
Base *ptr = new Derived();
|
||||
ptr->who();
|
||||
|
||||
// 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。
|
||||
// 因为Base有虚析构函数(virtual ~Base() {}),所以 delete
|
||||
// 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。
|
||||
delete ptr;
|
||||
|
||||
return 0;
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user