Init
This commit is contained in:
28
02-Note/读书笔记/深入理解c++11/第三章.md
Normal file
28
02-Note/读书笔记/深入理解c++11/第三章.md
Normal file
@@ -0,0 +1,28 @@
|
||||
#### 继承构造函数
|
||||
```
|
||||
struct A{
|
||||
void f(double i){}
|
||||
|
||||
A(){}
|
||||
|
||||
A(int i){}
|
||||
|
||||
A(float f){}
|
||||
};
|
||||
|
||||
struct B:A{
|
||||
using A:A;
|
||||
|
||||
using A:f;
|
||||
|
||||
void f(double i){}
|
||||
}
|
||||
|
||||
int main(){
|
||||
B b;//会调用A的对应构造函数
|
||||
b.f;//会调用A的函数
|
||||
}
|
||||
```
|
||||
不过使用了继承构造函数,就无法通过构造函数初始化派生类成员了。
|
||||
#### 用户自定义字面量
|
||||
可以定义出2.0f或者123_w之类的写法,表达瓦特与浮点
|
96
02-Note/读书笔记/深入理解c++11/第二章.md
Normal file
96
02-Note/读书笔记/深入理解c++11/第二章.md
Normal file
@@ -0,0 +1,96 @@
|
||||
#### 2.1.2 __func__
|
||||
__func__ 宏:返回函数名称 c++11兼容c99标准
|
||||
#### 2.1.4 变长参数宏以及__VA_ARGS__
|
||||
```
|
||||
#define LOG(...){\
|
||||
fprintf(stderr,"%s: Line %d:\t",__FINE__,__LINE__);\
|
||||
fprintf(stderr,__VA_ARGS__);\
|
||||
fprintf(stderr,"\n");\
|
||||
}
|
||||
|
||||
int main(){
|
||||
int x=3;
|
||||
LOG("x=%d",x);//2-1-5.cpp: line 12: x=3
|
||||
}
|
||||
```
|
||||
#### 2.4 宏__cplusplus
|
||||
可以用于判断是否是c++编译环境,同时也可以判断c++版本,防止c++11程序在c++98编译器下编译
|
||||
#### 2.5.1
|
||||
assert宏定义在cassert头文件中
|
||||
```
|
||||
assert(expr);
|
||||
```
|
||||
首先对expr求值,如果表达式为假,assert输出信息,并且终止程序运行。为真则什么也不做。
|
||||
|
||||
在开头使用(需要写在cassert头文件之前,因为本质上还是宏判断)
|
||||
```
|
||||
#define NDEBUG
|
||||
```
|
||||
可以跳过检测。在VC++里面,release会在全局定义NDEBUG。<br>
|
||||
既然定义了NDEBUG,还可以:
|
||||
```
|
||||
#ifndef
|
||||
//do any debug thing
|
||||
//__FILE__ 文件名字面值
|
||||
//__LINE__ 当前行号字面值
|
||||
//__TIME__ 文件编译时间字面值
|
||||
//__DATE__ 文件编译时间字面值
|
||||
//__func__ 函数名称 c++11兼容c99标准
|
||||
//用于打印debug信息
|
||||
#endif
|
||||
```
|
||||
## error用法
|
||||
```
|
||||
#ifndef UNIX
|
||||
#error This software requires the UNIX OS.
|
||||
#endif
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
#error C++ compiler required.
|
||||
#endif
|
||||
```
|
||||
#### 2.5.2 静态断言与static_assert
|
||||
编译期断言
|
||||
static_assert(false,"error string");
|
||||
#### 2.6 noexcept修饰符和noexcept操作符
|
||||
```
|
||||
void excpt_func() noexcept;//虽然这样函数就不会抛出异常,但是如果抛出了异常,程序会直接被终止
|
||||
|
||||
void excpt_func() noexcept(expr);//如果为true则不会抛出异常
|
||||
```
|
||||
#### 2.9 扩展的friend语法
|
||||
```
|
||||
class A{
|
||||
|
||||
};
|
||||
class B{
|
||||
friend class A;//都通过
|
||||
};
|
||||
class C{
|
||||
friend B;//c++11通过,c++98失败
|
||||
};
|
||||
```
|
||||
可以省略class直接声明类的友元,这个改动的意义在于类模板,不过使用内置对象时就会实例化成一个没有友元的类
|
||||
```
|
||||
class A;
|
||||
template <class T>
|
||||
class People{
|
||||
friend T;
|
||||
};
|
||||
```
|
||||
#### 模板函数的默认模板参数
|
||||
```
|
||||
template<typename T=int>
|
||||
void func(){
|
||||
|
||||
}
|
||||
```
|
||||
#### 2.12.1 外部模板
|
||||
在头文件中定义模板类的情况下,为了防止编译器给每个包含头文件的cpp文件生成代码,可以添加extern关键字。当然至少要有一个是不加extern的。
|
||||
```
|
||||
template <typename T> void func(T){
|
||||
|
||||
}
|
||||
|
||||
extern template void func<int>(int);
|
||||
```
|
11
02-Note/读书笔记/深入理解c++11/第五章.md
Normal file
11
02-Note/读书笔记/深入理解c++11/第五章.md
Normal file
@@ -0,0 +1,11 @@
|
||||
#### 5.1.3 强类型枚举
|
||||
```
|
||||
enum class Type {a,b,c,d};
|
||||
```
|
||||
优势:
|
||||
1. 强作用域
|
||||
2. 转换限制(禁止隐式转换)
|
||||
3. 可以指定底层类型
|
||||
```
|
||||
enum class Type : char {a,b,c,d};//指定了类型
|
||||
```
|
Reference in New Issue
Block a user