0%

C++11新特性

C++11新特性

nullptr

nullptr

1
2
3
4
5
6
7
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

0 \0 NULL nullptr 的差异和共同点

auto与decltype

decltype

右值引用

浅拷贝与深浅拷贝

浅拷贝 shollow copy
深拷贝 deep copy
指针成员与拷贝构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//浅拷贝
class HasPtrMem {
private:
int *intptr;
public:
HasPtrMem() :intptr(new int[100]) {}
~HasPtrMem() {
delete[] intptr;
}
};

int main(){
HasPtrMem a;
HasPtrMem b(a);
return 0
}

浅拷贝 默认拷贝构造函数是浅拷贝
main作用域结束时,依次调用 b,a的析构函数
b释放b.intptr的堆内存后,a.intptr就成为了一个 “悬挂指针”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//深拷贝
class HasPtrMem {
public:
int *intptr;
public:
HasPtrMem() :intptr(new int[100]) {
for (int i = 0; i < 100; i++) { intptr[i] = i; }
}
HasPtrMem(const HasPtrMem& _right) :intptr(new int[100]()) {
memcpy(intptr, _right.intptr,100);
}
~HasPtrMem() {
delete[] intptr;
}
};

深拷贝 调用拷贝构造函数时 会从堆中为b.intptr分配内存

移动语义和完美转发

移动构造函数

lambda表达式