Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
107 lines (77 loc) · 2.35 KB

File metadata and controls

107 lines (77 loc) · 2.35 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// Created by light on 19-11-3.
//
#include <iostream>
#include <complex>
using namespace std;
class Zoo {
public:
Zoo(int a, int b) : a(a), b(b) {
cout << "with param ctor \n";
}
Zoo() = default;
Zoo(const Zoo &) = delete; // copy ctor
Zoo(Zoo &&) = default; // move ctor
Zoo &operator=(const Zoo &) = default; // copy assignment
Zoo &operator=(const Zoo &&) = delete; // move assignment
// 一般函数可以使用=default或=delete?
// void fun1()= default; // error: ‘void Zoo::fun1()’ cannot be defaulted
void fun1() = delete; // compile ok
// 析构函数可以使用=default或=delete?
// ~Zoo()= delete; // ok,但是造成使用Zoo object出错
~Zoo() = default;
// 上述得出=delete可以使用在任何函数身上,而=default不可以使用在普通函数上
// 上述会联想到=0,=0只能用在virtual funciton上
private:
int a, b;
};
class Empty {
};
// 等价于 编译器给出的默认版本函数都是public且inline
class Empty1 {
public:
Empty1() {}
Empty1(const Empty1 &rhs) {}
~Empty1() {}
};
// no-copy
// copy ctor与copy assignment都delete掉
struct NoCopy {
NoCopy() = default;
NoCopy(const NoCopy &) = delete;
NoCopy &operator=(const NoCopy &) = delete;
~NoCopy() = default;
};
// no-dtor
struct NoDtor {
NoDtor() = default;
~NoDtor() = delete;
};
// 对no-copy的改进 将copy ctor与copy assignment 放到private ,但是可以对member与friend调用
// boost::noncopyable 就用到了PrivateCopy
class PrivateCopy {
private:
PrivateCopy(const PrivateCopy &) {};
PrivateCopy &operator=(const PrivateCopy &) {};
public:
PrivateCopy() = default;
~PrivateCopy() {};
};
// 继承了的都拥有PrivateCopy性质
class Foo : public PrivateCopy {
};
int main() {
Zoo z;
Zoo z1(1, 2);
// Zoo z2=z; // error copy ctor delete
z = z1;
complex<int> a; // 内部不带指针的可以不用写 big three 没有析构 有copy ctor与copy assignmetn 都是使用=default,没有自己写
string s; // 内部带指针的有big five
// NoDtor n; //error no-dtor 不能够自动delete
// NoDtor *p=new NoDtor; // ok
// delete p; // error no-dtor 不能够delete
Foo foo;
Foo foo1;
// foo = foo1; // 继承了的都拥有PrivateCopy性质
return 0;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.