support bazel complie this project and format code.

This commit is contained in:
zhangxing 2023-03-30 00:15:11 +08:00 committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

6
.gitignore vendored
View File

@ -1 +1,7 @@
com.sh com.sh
bazel-CPlusPlusThings
bazel-bin
bazel-out
bazel-testlogs
bazel-cplusplus_bazel
.vscode

112
.vscode/settings.json vendored Normal file
View 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
View File

@ -155,15 +155,15 @@
├── [输入输出重载.cpp](./practical_exercises/key_exercises/输入输出重载.cpp) ├── [输入输出重载.cpp](./practical_exercises/key_exercises/输入输出重载.cpp)
├── [输出格式.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 概况 #### 3.0 概况
C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。 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 - 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后可以正常通过 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 ```cpp
for(decl:col) { 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` 如果你自行定义了一个 `ctor`, 那么编译器就不会给你一个 `default ctor`
如果强制加上 `=default`, 就可以重新获得并使用 `default ctor`. 如果强制加上 `=default`, 就可以重新获得并使用 `default ctor`.
- Alias(化名) Template (template typedef) - Alias(化名) Template (template typedef)
- [alias.cpp](./c++2.0/c++11/alias.cpp) - [alias.cpp](./cpp2.0/cpp11/alias.cpp)
- [template_template.cpp](./c++2.0/c++11/template_template.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` - (2) 关联式容器包括:`set/multiset`,`map/multimap`
- (3) 无序容器(C++2.0 新引入,更换原先 `hash_xxx``unordered_xxx`)包括:`unordered_map/unordered_multimap,unordered_set/unordered_multiset` - (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 学习资料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) #### 8.1 [极客时间《现代 C++ 实战 30 讲》](https://time.geekbang.org/channel/home)
- [堆、栈、RAIIC++ 里该如何管理资源?](./learn_class/modern_C++_30/RAII) - [堆、栈、RAIIC++ 里该如何管理资源?](./learn_class/modern_cpp_30/RAII)
- [](./learn_class/modern_++_30/RAII/heap.cpp) - [](./learn_class/modern_++_30/RAII/heap.cpp)
- [](./learn_class/modern_C++_30/RAII/stack.cpp) - [](./learn_class/modern_cpp_30/RAII/stack.cpp)
- [RAII](./learn_class/modern_C++_30/RAII/RAII.cpp) - [RAII](./learn_class/modern_cpp_30/RAII/RAII.cpp)
- [自己动手,实现 C++ 的智能指针](./learn_class/modern_C++_30/smart_ptr) - [自己动手,实现 C++ 的智能指针](./learn_class/modern_cpp_30/smart_ptr)
- [auto_ptr、scope_ptr](./learn_class/modern_C++_30/smart_ptr/auto_scope.cpp) - [auto_ptr、scope_ptr](./learn_class/modern_cpp_30/smart_ptr/auto_scope.cpp)
- [unique_ptr](./learn_class/modern_C++_30/smart_ptr/unique_ptr.cpp) - [unique_ptr](./learn_class/modern_cpp_30/smart_ptr/unique_ptr.cpp)
- [shared_ptr](./learn_class/modern_C++_30/smart_ptr/shared_ptr.cpp) - [shared_ptr](./learn_class/modern_cpp_30/smart_ptr/shared_ptr.cpp)
- [右值和移动究竟解决了什么问题?](./learn_class/modern_C++_30/reference) - [右值和移动究竟解决了什么问题?](./learn_class/modern_cpp_30/reference)
- [左值与右值](./learn_class/modern_C++_30/reference/reference.cpp) - [左值与右值](./learn_class/modern_cpp_30/reference/reference.cpp)
- [延长声明周期](./learn_class/modern_C++_30/reference/lifetime.cpp) - [延长声明周期](./learn_class/modern_cpp_30/reference/lifetime.cpp)
- [引用折叠](./learn_class/modern_C++_30/reference/collapses.cpp) - [引用折叠](./learn_class/modern_cpp_30/reference/collapses.cpp)
- [完美转发](./learn_class/modern_C++_30/reference/forward.cpp) - [完美转发](./learn_class/modern_cpp_30/reference/forward.cpp)
- [不要返回本地变量的引用](./learn_class/modern_C++_30/reference/don'treturnReference.cpp) - [不要返回本地变量的引用](./learn_class/modern_cpp_30/reference/don'treturnReference.cpp)
- [容器 1](./learn_class/modern_C++_30/container1) - [容器 1](./learn_class/modern_cpp_30/container1)
- [容器 2](./learn_class/modern_C++_30/container2) - [容器 2](./learn_class/modern_cpp_30/container2)
- [异常](./learn_class/modern_C++_30/exception) - [异常](./learn_class/modern_cpp_30/exception)
- [字面量、静态断言和成员函数说明符](./learn_class/modern_C++_30/literalAssert) - [字面量、静态断言和成员函数说明符](./learn_class/modern_cpp_30/literalAssert)
- [是不是应该返回对象?](./learn_class/modern_C++_30/returnObj) - [是不是应该返回对象?](./learn_class/modern_cpp_30/returnObj)
- [编译期多态:泛型编程和模板入门](./learn_class/modern_C++_30/compilerpoly) - [编译期多态:泛型编程和模板入门](./learn_class/modern_cpp_30/compilerpoly)
- [译期能做些什么?一个完整的计算世界](./learn_class/modern_C++_30/compilercompute) - [译期能做些什么?一个完整的计算世界](./learn_class/modern_cpp_30/compilercompute)
- [SFINAE不是错误的替换失败是怎么回事?](./learn_class/modern_C++_30/SFINAE) - [SFINAE不是错误的替换失败是怎么回事?](./learn_class/modern_cpp_30/SFINAE)
- [constexpr一个常态的世界](./learn_class/modern_C++_30/constexpr) - [constexpr一个常态的世界](./learn_class/modern_cpp_30/constexpr)
- [函数对象和 lambda进入函数式编程](./learn_class/modern_C++_30/functionLambda) - [函数对象和 lambda进入函数式编程](./learn_class/modern_cpp_30/functionLambda)
- [内存模型和 atomic理解并发的复杂性](./learn_class/modern_C++_30/memorymodel_atomic) - [内存模型和 atomic理解并发的复杂性](./learn_class/modern_cpp_30/memorymodel_atomic)
### 9.工具篇 ### 9.工具篇
@ -376,20 +376,20 @@ map<int, int> mp{
### 12.赞助 ### 12.关注
如果觉得不错,赞助我吧~ 如果觉得不错,关注我吧~
<table> <table>
<tbody> <tbody>
<tr> <tr>
<th align="center" style="height=200 width="200"> <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>
<th align="center" style="height=200 width="200"> <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> </th>
</tr> </tr>
</tbody> </tbody>

View File

@ -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) #### 6.1 [Chinese Name:极客时间《现代 C++ 实战 30 讲》](https://time.geekbang.org/channel/home)
- [heap、stack、RAIIHow to manage resources for C ++ ?](./learn_class/modern_C++_30/RAII) - [heap、stack、RAIIHow to manage resources for C ++ ?](./learn_class/modern_cpp_30/RAII)
- [heap](./modern_++_30/RAII/heap.cpp) - [heap](./modern_++_30/RAII/heap.cpp)
- [stack](./learn_class/modern_C++_30/RAII/stack.cpp) - [stack](./learn_class/modern_cpp_30/RAII/stack.cpp)
- [RAII](./learn_class/modern_C++_30/RAII/RAII.cpp) - [RAII](./learn_class/modern_cpp_30/RAII/RAII.cpp)
- [Implementing smart pointers for C ++](./learn_class/modern_C++_30/smart_ptr) - [Implementing smart pointers for C ++](./learn_class/modern_cpp_30/smart_ptr)
- [auto_ptr、scope_ptr](./learn_class/modern_C++_30/smart_ptr/auto_scope.cpp) - [auto_ptr、scope_ptr](./learn_class/modern_cpp_30/smart_ptr/auto_scope.cpp)
- [unique_ptr](./learn_class/modern_C++_30/smart_ptr/unique_ptr.cpp) - [unique_ptr](./learn_class/modern_cpp_30/smart_ptr/unique_ptr.cpp)
- [shared_ptr](./learn_class/modern_C++_30/smart_ptr/shared_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_C++_30/reference) - [What exactly does r value and move solve](./learn_class/modern_cpp_30/reference)
- [L value and R value](./learn_class/modern_C++_30/reference/reference.cpp) - [L value and R value](./learn_class/modern_cpp_30/reference/reference.cpp)
- [Extend the declaration cycle](./learn_class/modern_C++_30/reference/lifetime.cpp) - [Extend the declaration cycle](./learn_class/modern_cpp_30/reference/lifetime.cpp)
- [Reference folding](./learn_class/modern_C++_30/reference/collapses.cpp) - [Reference folding](./learn_class/modern_cpp_30/reference/collapses.cpp)
- [Perfect forward](./learn_class/modern_C++_30/reference/forward.cpp) - [Perfect forward](./learn_class/modern_cpp_30/reference/forward.cpp)
- [Do not return Reference](./learn_class/modern_C++_30/reference/don'treturnReference.cpp) - [Do not return Reference](./learn_class/modern_cpp_30/reference/don'treturnReference.cpp)
- [Container 1](./learn_class/modern_C++_30/container1) - [Container 1](./learn_class/modern_cpp_30/container1)
- [Container 2](./learn_class/modern_C++_30/container2) - [Container 2](./learn_class/modern_cpp_30/container2)
- [Exception](./learn_class/modern_C++_30/exception) - [Exception](./learn_class/modern_cpp_30/exception)
- [Literal、Static Assertion And Member Function Specifier](./learn_class/modern_C++_30/literalAssert) - [Literal、Static Assertion And Member Function Specifier](./learn_class/modern_cpp_30/literalAssert)
- [Return Object](./learn_class/modern_C++_30/returnObj)c - [Return Object](./learn_class/modern_cpp_30/returnObj)c
- [Getting started with generic programming and templates](./learn_class/modern_C++_30/compilerpoly) - [Getting started with generic programming and templates](./learn_class/modern_cpp_30/compilerpoly)
- [A whole Compiler Compute World](./learn_class/modern_C++_30/compilercompute) - [A whole Compiler Compute World](./learn_class/modern_cpp_30/compilercompute)
- [SFINAEWhat is it if it is not replace error?](./learn_class/modern_C++_30/SFINAE) - [SFINAEWhat is it if it is not replace error?](./learn_class/modern_cpp_30/SFINAE)
- [constexprA Normal World](./learn_class/modern_C++_30/constexpr) - [constexprA Normal World](./learn_class/modern_cpp_30/constexpr)
- [Function object and LambdafunctionLambda](./learn_class/modern_C++_30/functionLambda) - [Function object and LambdafunctionLambda](./learn_class/modern_cpp_30/functionLambda)
- [Memory Model and AtomicUnderstangding the complexity of concurrency](./learn_class/modern_C++_30/memorymodel_atomic) - [Memory Model and AtomicUnderstangding the complexity of concurrency](./learn_class/modern_cpp_30/memorymodel_atomic)
### 7.Tools ### 7.Tools

0
WORKSPACE Normal file
View File

View 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"],
)

View File

@ -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.实现抽象类 ## 2.实现抽象类

View File

@ -1,27 +1,28 @@
/** /**
* @file abstract.cpp * @file abstract.cpp
* @brief /使 * @brief
* /使
* *
* @author * @author
* @version v1 * @version v1
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
class A { class A {
public: public:
virtual void f() = 0; // 纯虚函数 virtual void f() = 0; // 纯虚函数
void g(){ this->f(); } void g() { this->f(); }
A(){} A() {}
}; };
class B:public A{ class B : public A {
public: public:
void f(){ cout<<"B:f()"<<endl;} void f() { cout << "B:f()" << endl; }
}; };
int main(){ int main() {
B b; B b;
b.g(); b.g();
return 0; return 0;
} }

View 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 */
};

View File

@ -6,27 +6,24 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
class Base class Base {
{ int x;
int x;
public: public:
virtual void fun() = 0; virtual void fun() = 0;
int getX() { return x; } int getX() { return x; }
}; };
class Derived: public Base class Derived : public Base {
{ public:
int y; void fun() { cout << "fun() called"; } // 实现了fun()函数
public:
void fun() { cout << "fun() called"; } // 实现了fun()函数
}; };
int main(void) int main(void) {
{ Derived d;
Derived d; d.fun();
d.fun(); return 0;
return 0;
} }

View File

@ -6,23 +6,21 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
/** /**
* @brief * @brief
*/ */
class Test class Test {
{ int x;
int x;
public: public:
virtual void show() = 0; virtual void show() = 0;
int getX() { return x; } int getX() { return x; }
}; };
int main(void) int main(void) {
{ Test t; // error! 不能创建抽象类的对象
Test t; //error! 不能创建抽象类的对象 return 0;
return 0;
} }

View File

@ -6,33 +6,28 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
/** /**
* @brief * @brief
*/ */
class Base class Base {
{ int x;
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived: public Base
{
public: public:
void show() { cout << "In Derived \n"; } virtual void show() = 0;
Derived(){} int getX() { return x; }
}; };
int main(void) class Derived : public Base {
{ public:
//Base b; //error! 不能创建抽象类的对象 void show() { cout << "In Derived \n"; }
//Base *b = new Base(); error! Derived() {}
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象 };
bp->show(); int main(void) {
return 0; // Base b; //error! 不能创建抽象类的对象
// Base *b = new Base(); error!
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
bp->show();
return 0;
} }

View File

@ -6,24 +6,23 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
class Base class Base {
{ int x;
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;
}
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived : public Base {
public:
// void show() { }
};
int main(void) {
Derived
d; // error!
// 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
return 0;
}

View File

@ -6,30 +6,29 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
// An abstract class with constructor // An abstract class with constructor
class Base class Base {
{ protected:
protected: int x;
int x;
public: public:
virtual void fun() = 0; virtual void fun() = 0;
Base(int i) { x = i; } Base(int i) { x = i; }
}; };
class Derived: public Base class Derived : public Base {
{ int y;
int y;
public: public:
Derived(int i, int j):Base(i) { y = j; } Derived(int i, int j) : Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " << y; } void fun() { cout << "x = " << x << ", y = " << y; }
}; };
int main(void) int main(void) {
{ Derived d(4, 5);
Derived d(4, 5); d.fun();
d.fun(); return 0;
return 0;
} }

View File

@ -7,24 +7,23 @@
* @version v1 * @version v1
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
class Base { class Base {
public: public:
Base() { cout << "Constructor: Base" << endl; } Base() { cout << "Constructor: Base" << endl; }
virtual ~Base() { cout << "Destructor : Base" << endl; } virtual ~Base() { cout << "Destructor : Base" << endl; }
}; };
class Derived: public Base { class Derived : public Base {
public: public:
Derived() { cout << "Constructor: Derived" << endl; } Derived() { cout << "Constructor: Derived" << endl; }
~Derived() { cout << "Destructor : Derived" << endl; } ~Derived() { cout << "Destructor : Derived" << endl; }
}; };
int main() { int main() {
Base *Var = new Derived(); Base *Var = new Derived();
delete Var; delete Var;
return 0; return 0;
} }

View File

@ -8,27 +8,25 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
class A class A {
{
private: private:
int a; int a;
public: public:
virtual void show()=0; // 纯虚函数 virtual void show() = 0; // 纯虚函数
}; };
int main() {
/*
* 1. 使
* 2. ->
*/
A a; // error 抽象类,不能创建对象
int main() A *a1; // ok 可以定义抽象类的指针
{
/*
* 1. 使
* 2. ->
*/
A a; // error 抽象类,不能创建对象
A *a1; // ok 可以定义抽象类的指针 A *a2 = new A(); // error, A是抽象类不能创建对象
A *a2 = new A(); // error, A是抽象类不能创建对象
} }

View File

@ -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 */
};

View 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"],
)

View File

@ -6,7 +6,8 @@ int main()
int x = 7; int x = 7;
/* Some big code in between and let's say x /* Some big code in between and let's say x
* is accidentally changed to 9 */ * is accidentally changed to 9
*/
x = 9; x = 9;
// Programmer assumes x to be 7 in rest of the code // Programmer assumes x to be 7 in rest of the code

View File

@ -11,7 +11,6 @@
#include<assert.h> #include<assert.h>
int main(){ int main(){
int x=7;
assert(x==5); assert(x==5);
return 0; return 0;
} }

7
basic_content/bit/BUILD Normal file
View 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"],
)

View File

@ -108,7 +108,7 @@ struct stuff
这里 stuff.field1 与 stuff.field2 之间有一个 2 Bits 的空隙stuff.field3 则存储在下一个 unsigned int 中,该结构现在大小为 3 * 32 = 96 Bits。 这里 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
View 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;
}

View File

@ -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;
}

View 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.

View File

@ -9,34 +9,31 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class A class A {
{ public:
public: virtual void f() //虚函数实现
virtual void f()//虚函数实现 {
{ cout << "Base A::f() " << endl;
cout << "Base A::f() " << endl; }
}
}; };
class B:public A // 必须为共有继承否则后面调用不到class默认为私有继承 class B : public A // 必须为共有继承否则后面调用不到class默认为私有继承
{ {
public: public:
virtual void f()//虚函数实现,子类中virtual关键字可以没有 virtual void f() //虚函数实现,子类中virtual关键字可以没有
{ {
cout << "Derived B::f() " << endl; cout << "Derived B::f() " << endl;
} }
}; };
int main() {
A a; //基类对象
B b; //派生类对象
int main() A *pa = &a; //父类指针指向父类对象
{ pa->f(); //调用父类的函数
A a;//基类对象
B b;//派生类对象
A* pa = &a;//父类指针指向父类对象 pa = &b; //父类指针指向子类对象,多态实现
pa->f();//调用父类的函数 pa->f(); //调用派生类同名函数
return 0;
pa = &b; //父类指针指向子类对象,多态实现
pa->f();//调用派生类同名函数
return 0;
} }

Binary file not shown.

17
basic_content/const/BUILD Normal file
View 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"]
)

View File

@ -298,41 +298,42 @@ public:
int getCount() const; int getCount() const;
}; };
//main.cpp // apple.cpp
#include<iostream> Apple::Apple(int i) : apple_number(i)
#include"apple.cpp"
using namespace std;
Apple::Apple(int i):apple_number(i)
{ {
} }
int Apple::add(){ int Apple::add(int num)
take(1); {
take(num);
return 0; return 0;
} }
int Apple::add(int num) const{ int Apple::add(int num) const
{
take(num); take(num);
return num; return 0;
} }
void Apple::take(int num) const void Apple::take(int num) const
{ {
cout<<"take func "<<num<<endl; std::cout << "take func " << num << std::endl;
} }
int Apple::getCount() const int Apple::getCount() const
{ {
take(1); take(1);
add(); // error // add(); //error
return apple_number; return apple_number;
} }
int main(){ int main()
{
Apple a(2); Apple a(2);
cout<<a.getCount()<<endl; cout << a.getCount() << endl;
a.add(10); a.add(10);
const Apple b(3);
b.add(100);
return 0; 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成员函数。 此时报错上面getCount()方法中调用了一个add方法而add方法并非const修饰所以运行报错。也就是说const成员函数只能访问const成员函数。

View 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",
],
)

View File

@ -1,15 +1,19 @@
class Apple #include "apple.h"
{ #include <iostream>
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;
};
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;
}

View 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;
};

View File

@ -1,31 +1,11 @@
#include<iostream> #include "apple.h"
#include"apple.cpp" #include <iostream>
using namespace std; using namespace std;
Apple::Apple(int i) int main() {
{ Apple a(2);
cout << a.getCount() << endl;
} a.add(10);
int Apple::add(int num){ const Apple b(3);
take(num); b.add(100);
} return 0;
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;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
} }

View 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",
],
)

View File

@ -1,13 +1,20 @@
class Apple #include "apple.h"
{ #include <iostream>
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;
};
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;
}

View 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;
};

View File

@ -1,32 +1,12 @@
#include<iostream> #include "apple.h"
#include"apple.cpp" #include <iostream>
using namespace std; using namespace std;
Apple::Apple(int i):apple_number(i) int main() {
{ Apple a(2);
cout << a.getCount() << endl;
} a.add(10);
int Apple::add(int num){ const Apple b(3);
take(num); b.add(100);
} return 0;
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;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
} }

View 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"]
)

View File

@ -1,13 +1,22 @@
class Apple #include "apple.h"
{ #include <iostream>
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;
};
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;
}

View 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;
};

View File

@ -1,38 +1,12 @@
#include<iostream> #include "apple.h"
#include"apple.cpp" #include <iostream>
using namespace std;
const int Apple::apple_number=10;
#include<iostream>
#include"apple.cpp"
using namespace std; using namespace std;
Apple::Apple(int i):apple_number(i) int main() {
{ Apple a(2);
cout << a.getCount() << endl;
} a.add(10);
int Apple::add(){ // const Apple b(3);
take(1); // b.add(); // error
return 0; 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;
a.add(10);
// const Apple b(3);
// b.add(); // error
return 0;
} }

View 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"]
)

View File

@ -1,14 +1,22 @@
class Apple #include "apple.h"
{ #include <iostream>
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;
};
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;
}

View 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;
};

View File

@ -1,34 +1,11 @@
#include<iostream> #include "apple.h"
#include"apple.cpp" #include <iostream>
using namespace std; int main() {
const int Apple::apple_number=10; Apple a(2);
int Apple::ap=666; std::cout << a.getCount() << std::endl;
Apple::Apple(int i) std::cout << a.ap << std::endl;
{ a.add(10);
const Apple b(3);
} b.add(100);
int Apple::add(int num){ return 0;
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;
a.add(10);
const Apple b(3);
b.add(100);
return 0;
} }

View File

@ -1,11 +1,9 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
void f(const int i){ void f(const int i) {
i=10; // error: assignment of read-only parameter i i = 10; // error: assignment of read-only parameter i
cout<<i<<endl; cout << i << endl;
} }
int main(){ int main() { f(1); }
f(1);
}

View File

@ -1,8 +1,8 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
const int b = 10; const int b = 10;
b = 0; //error b = 0; // error
const string s = "helloworld"; const string s = "helloworld";
const int i,j=0; const int i, j = 0;
} }

View 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"]
)

View File

@ -1 +1 @@
extern const int ext=12; extern const int ext = 12;

View File

@ -1,11 +1,8 @@
#include<iostream> #include <iostream>
/** /**
* by * by
* compile: g++ -o file const_file2.cpp const_file1.cpp * compile: g++ -o file const_file2.cpp const_file1.cpp
* execute: ./file * execute: ./file
*/ */
extern const int ext; extern const int ext;
int main(){ int main() { std::cout << ext << std::endl; }
std::cout<<ext<<std::endl;
}

View File

@ -1,11 +1,8 @@
#include<iostream> #include <iostream>
/** /**
* by * by
* compile: g++ -o file file2.cpp file1.cpp * compile: g++ -o file file2.cpp file1.cpp
* execute: ./file * execute: ./file
*/ */
extern int ext; extern int ext;
int main(){ int main() { std::cout << (ext + 10) << std::endl; }
std::cout<<(ext+10)<<std::endl;
}

View 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"]
)

View File

@ -1,8 +1,7 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main() {
int main(){ const int *ptr;
const int *ptr; *ptr = 10; // error
*ptr=10; //error
} }

View File

@ -1,9 +1,8 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main() {
int main(){ const int p = 10;
const int p = 10; const void *vp = &p;
const void *vp = &p; void *vp = &p; // error
void *vp = &p; //error
} }

View File

@ -1,12 +1,11 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
const int *ptr; const int *ptr;
int val = 3; int val = 3;
ptr = &val; //ok ptr = &val; // ok
int *ptr1 = &val; int *ptr1 = &val;
*ptr1=4; *ptr1 = 4;
cout<<*ptr<<endl; cout << *ptr << endl;
} }

View 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"]
)

View File

@ -1,10 +1,9 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
int num = 0;
int num=0; int *const ptr = &num; // const指针必须初始化且const指针的值不能修改
int * const ptr=&num; //const指针必须初始化且const指针的值不能修改 int *t = &num;
int * t = &num; *t = 1;
*t = 1; cout << *ptr << endl;
cout<<*ptr<<endl;
} }

View File

@ -1,8 +1,7 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
const int num = 0;
const int num=0; int *const ptr = &num; // error! const int* -> int*
int * const ptr=&num; //error! const int* -> int* cout << *ptr << endl;
cout<<*ptr<<endl;
} }

View File

@ -1,8 +1,7 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
const int num = 10;
const int num=10; const int *const ptr = &num; // error! const int* -> int*
const int * const ptr=&num; //error! const int* -> int* cout << *ptr << endl;
cout<<*ptr<<endl;
} }

View 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"]
)

View File

@ -1,10 +1,8 @@
#include<iostream> #include <iostream>
using namespace std; using namespace std;
int main(){ int main() {
const int p = 3;
const int p = 3; const int *const ptr = &p;
const int * const ptr = &p; cout << *ptr << endl;
cout<<*ptr<<endl;
} }

View 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.

View File

@ -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 <iostream>
#include <vector> #include <vector>
using namespace std; using namespace std;
@ -14,47 +6,42 @@ using namespace std;
*/ */
template <typename T> template <typename T>
auto multiply(T x, T y)->decltype(x*y) auto multiply(T x, T y) -> decltype(x * y) {
{ return x * y;
return x*y;
} }
int main() int main() {
{ int nums[] = {1, 2, 3, 4};
int nums[] = {1,2,3,4}; vector<int> vec(nums, nums + 4);
vector<int> vec(nums,nums+4); vector<int>::iterator it;
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++) for (it = vec.begin(); it != vec.end(); it++)
cout<<*it<<" "; cout << *it << " ";
cout<<endl; cout << endl;
using nullptr_t = decltype(nullptr);
nullptr_t nu;
int *p = NULL;
if (p == nu)
cout << "NULL" << endl;
using nullptr_t = decltype(nullptr); typedef decltype(vec.begin()) vectype;
nullptr_t nu;
int * p =NULL;
if(p==nu)
cout<<"NULL"<<endl;
for (vectype i = vec.begin(); i != vec.end(); i++)
cout << *i << " ";
cout << endl;
typedef decltype(vec.begin()) vectype; /**
*
*/
struct {
int d;
double b;
} anon_s;
for(vectype i=vec.begin();i!=vec.end();i++) decltype(anon_s) as{1, 2.0}; // 定义了一个上面匿名的结构体
cout<<*i<<" ";
cout<<endl;
/** cout << multiply(11, 2) << ":" << as.b << endl;
*
*/
struct
{
int d ;
double b;
}anon_s;
decltype(anon_s) as; // 定义了一个上面匿名的结构体 return 0;
cout<<multiply(11,2)<<endl;
return 0;
} }

16
basic_content/enum/BUILD Normal file
View 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"]
)

View File

@ -15,14 +15,22 @@
- 作用域不受限,会容易引起命名冲突。例如下面无法编译通过的: - 作用域不受限,会容易引起命名冲突。例如下面无法编译通过的:
```c++ ```c++
#include <iostream> enum Color
using namespace std; {
RED,
enum Color {RED,BLUE}; BLUE
enum Feeling {EXCITED,BLUE}; };
enum Feeling
{
EXCITED,
BLUE
};
int main() int main()
{ {
Color a = BLUE; // error
Feeling b = EXCITED;
std::cout << a << ":" << b << std::endl;
return 0; return 0;
} }
``` ```

Binary file not shown.

View File

@ -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> #include <iostream>
using namespace std; using namespace std;
/** /**
* @brief namespace解决作用域不受限 * @brief namespace解决作用域不受限
*/ */
namespace Color namespace Color {
{ enum Type { RED = 15, YELLOW, BLUE };
enum Type
{
RED=15,
YELLOW,
BLUE
};
}; };
/** /**
@ -32,56 +18,40 @@ namespace Color
* 访 * 访
* *
*/ */
struct Color1 struct Color1 {
{ enum Type { RED = 102, YELLOW, BLUE };
enum Type
{
RED=102,
YELLOW,
BLUE
};
}; };
/** /**
* @brief C++11 * @brief C++11
* enum class Color2:int * enum class Color2:int
*/ */
enum class Color2 enum class Color2 { RED = 2, YELLOW, BLUE };
{
RED=2,
YELLOW,
BLUE
};
enum class Color3:char; // 前向声明 enum class Color3 : char; // 前向声明
// 定义 // 定义
enum class Color3:char enum class Color3 : char { RED = 'r', BLUE };
{
RED='r',
BLUE
};
int main() int main() {
{ // 定义新的枚举变量
// 定义新的枚举变量 Color::Type c = Color::RED;
Color::Type c = Color::RED; cout << c << endl;
cout<<c<<endl; /**
/** *
* * using namespace Color; // 定义新的枚举变量
* using namespace Color; // 定义新的枚举变量 * Type c = RED;
* Type c = RED; */
*/ Color1 c1;
Color1 c1; cout << c1.RED << endl;
cout<<c1.RED<<endl;
Color1::Type c11 = Color1::BLUE; Color1::Type c11 = Color1::BLUE;
cout<<c11<<endl; cout << c11 << endl;
Color2 c2 = Color2::RED; Color2 c2 = Color2::RED;
cout << static_cast<int>(c2) << endl; cout << static_cast<int>(c2) << endl;
char c3 = static_cast<char>(Color3::RED); char c3 = static_cast<char>(Color3::RED);
cout<<c3<<endl; cout << c3 << endl;
return 0; return 0;
} }

View File

@ -1,10 +1,12 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
enum Color {RED,BLUE}; enum Color { RED, BLUE };
enum Feeling {EXCITED,BLUE}; enum Feeling { EXCITED, BLUE };
int main() int main() {
{ Color a = BLUE; // error
return 0; Feeling b = EXCITED;
std::cout << a << ":" << b << std::endl;
return 0;
} }

View 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.

View File

@ -2,45 +2,50 @@
using namespace std; using namespace std;
struct A struct A {
{ A(int) {}
A(int) { } operator bool() const { return true; }
operator bool() const { return true; }
}; };
struct B struct B {
{ explicit B(int) {}
explicit B(int) {} explicit operator bool() const { return true; }
explicit operator bool() const { return true; }
}; };
void doA(A a) {} void doA(A a) {}
void doB(B b) {} void doB(B b) {}
int main() int main() {
{ A a1(1); // OK直接初始化
A a1(1); // OK直接初始化 A a2 = 1; // OK复制初始化
A a2 = 1; // OK复制初始化 A a3{1}; // OK直接列表初始化
A a3{ 1 }; // OK直接列表初始化 A a4 = {1}; // OK复制列表初始化
A a4 = { 1 }; // OK复制列表初始化 A a5 = (A)1; // OK允许 static_cast 的显式转换
A a5 = (A)1; // OK允许 static_cast 的显式转换 doA(1); // OK允许从 int 到 A 的隐式转换
doA(1); // OK允许从 int 到 A 的隐式转换 if (a1)
if (a1); // OK使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 ; // OK使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
bool a6(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 a7 = a1; // OK使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
bool a8 = static_cast<bool>(a1); // OK static_cast 进行直接初始化 bool a8 = static_cast<bool>(a1); // OK static_cast 进行直接初始化
B b1(1); // OK直接初始化 B b1(1); // OK直接初始化
// B b2 = 1; // 错误:被 explicit 修饰构造函数的对象不可以复制初始化 // B b2 = 1; // 错误:被 explicit
B b3{ 1 }; // OK直接列表初始化 // 修饰构造函数的对象不可以复制初始化
// B b4 = { 1 }; // 错误:被 explicit 修饰构造函数的对象不可以复制列表初始化 B b3{1}; // OK直接列表初始化
B b5 = (B)1; // OK允许 static_cast 的显式转换 // B b4 = { 1 }; // 错误:被 explicit
// doB(1); // 错误:被 explicit 修饰构造函数的对象不可以从 int 到 B 的隐式转换 // 修饰构造函数的对象不可以复制列表初始化
if (b1); // OK被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 B b5 = (B)1; // OK允许 static_cast 的显式转换
bool b6(b1); // OK被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 // doB(1); // 错误:被 explicit
// bool b7 = b1; // 错误:被 explicit 修饰转换函数 B::operator bool() 的对象不可以隐式转换 // 修饰构造函数的对象不可以从 int 到 B 的隐式转换
bool b8 = static_cast<bool>(b1); // OKstatic_cast 进行直接初始化 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); // OKstatic_cast 进行直接初始化
return 0; return 0;
} }

View File

@ -82,7 +82,7 @@ extern "C" {
#include "add.h" #include "add.h"
} }
int main() { int main() {
add(2,3); std::cout << add(2, 3) << std::endl;
return 0; return 0;
} }
``` ```
@ -99,6 +99,8 @@ gcc -c add.c
g++ add.cpp add.o -o main g++ add.cpp add.o -o main
``` ```
如果使用bazel编译运行:`bazel run basic_content/extern/extern_c++:main`
而通常为了C代码能够通用即既能被C调用又能被C++调用,头文件通常会有如下写法: 而通常为了C代码能够通用即既能被C调用又能被C++调用,头文件通常会有如下写法:
```c ```c

15
basic_content/extern/extern_c++/BUILD vendored Normal file
View 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"]
)

View File

@ -1,9 +1,9 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
extern "C" { extern "C" {
#include "add.h" #include "add.h"
} }
int main() { int main() {
add(2,3); std::cout << add(2, 3) << std::endl;
return 0; return 0;
} }

View File

@ -1,4 +1,4 @@
#ifndef ADD_H #ifndef ADD_H
#define ADD_H #define ADD_H
extern int add(int x,int y); extern int add(int x, int y);
#endif #endif

Binary file not shown.

Binary file not shown.

14
basic_content/extern/extern_c/BUILD vendored Normal file
View 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"],
)

View File

@ -1,5 +1,3 @@
#include "add.h" #include "add.h"
int add(int x,int y) { int add(int x, int y) { return x + y; }
return x+y;
}

View File

@ -1,6 +1,6 @@
#ifndef ADD_H #ifndef ADD_H
#define ADD_H #define ADD_H
extern "C" { extern "C" {
int add(int x,int y); int add(int x, int y);
} }
#endif #endif

Binary file not shown.

Binary file not shown.

View 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.

View File

@ -2,27 +2,23 @@
using namespace std; using namespace std;
class A class A {
{
public: public:
A(int _a):a(_a){}; A(int _a) : a(_a){};
friend class B; friend class B;
private: private:
int a; int a;
}; };
class B class B {
{
public: public:
int getb(A ca) { int getb(A ca) { return ca.a; };
return ca.a;
};
}; };
int main() int main() {
{ A a(3);
A a(3); B b;
B b; cout << b.getb(a) << endl;
cout<<b.getb(a)<<endl; return 0;
return 0;
} }

Binary file not shown.

View File

@ -1,33 +1,20 @@
/**
* @file friend_func.cpp
* @brief
* @author
* @version v1
* @date 2019-08-06
*/
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class A class A {
{
public: public:
A(int _a):a(_a){}; A(int _a) : a(_a){};
friend int geta(A &ca); ///< 友元函数 friend int geta(A &ca); ///< 友元函数
private: private:
int a; int a;
}; };
int geta(A &ca) int geta(A &ca) { return ca.a; }
{
return ca.a;
}
int main() int main() {
{ A a(3);
A a(3); cout << geta(a) << endl;
cout<<geta(a)<<endl;
return 0; return 0;
} }

View 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.

View File

@ -6,11 +6,12 @@
* @date 2019-07-20 * @date 2019-07-20
*/ */
#include<iostream> #include <iostream>
using namespace std; using namespace std;
/** /**
* @brief pFunint的函数的指针 * @brief
* pFunint的函数的指针
*/ */
void (*pFun)(int); void (*pFun)(int);
@ -19,15 +20,12 @@ void (*pFun)(int);
*/ */
typedef void (*func)(void); typedef void (*func)(void);
void myfunc(void) void myfunc(void) { cout << "asda" << endl; }
{
cout<<"asda"<<endl;
}
void glFun(int a){ cout<<a<<endl;} void glFun(int a) { cout << a << endl; }
int main(){ int main() {
func pfun = myfunc;/*赋值*/ func pfun = myfunc; /*赋值*/
pfun();/*调用*/ pfun(); /*调用*/
pFun = glFun; pFun = glFun;
(*pFun)(2); (*pFun)(2);
} }

View 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"]
)

View File

@ -27,7 +27,7 @@ public:
{ {
}; };
void f1(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字 void f2(int x); ///< 声明后要想成为内联函数必须在定义处加inline关键字
}; };
``` ```
@ -128,7 +128,7 @@ int main()
delete ptr; delete ptr;
ptr = nullptr; ptr = nullptr;
system("pause");
return 0; return 0;
} }
``` ```

Some files were not shown because too many files have changed in this diff Show More