-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoops1.cpp
96 lines (76 loc) · 1.71 KB
/
oops1.cpp
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
#include <iostream>
// class A{
// public:
// A(){
// std::cout << "A constructor\n";
// }
// };
// class B: public A{
// public:
// B(){
// std::cout << "B constructor\n";
// }
// class C{
// public:
// C(){
// std::cout << "C constructor\n";
// }
// };
// C c;
// };
class Base{
public:
Base(int a, float b){
std::cout << "in Base a: " << a << " b: " << b << '\n';
}
Base(const Base& rhs){
if(this == &rhs){
return;
}
a = rhs.a;
b = rhs.b;
}
Base(Base&& rhs){
std::cout << "move constructor is called\n";
if(this == &rhs){
return;
}
a = std::move(rhs.a);
b = std::move(rhs.b);
}
Base & operator=(const Base& rhs){
if(this == &rhs){
return *this;
}
a = rhs.a;
b = rhs.b;
return *this;
}
Base & operator=(Base&& rhs){
std::cout << "Move operator is called...\n";
if(this == &rhs){
return *this;
}
a = std::move(rhs.a);
b = std::move(rhs.b);
return *this;
}
int a = 0;
float b = 0.0;
};
int main(){
Base b(1,2);
Base b1(b);
Base b2(b);
b2 = std::move(b);
Base b3 = std::move(b2);
Base b4 = b3;
}
// if a parameterized constructor is defined, copy constructor, move constructor,
//copy assignment and move assignment operators are provided by compiler...
// if a copy constructor is provided no default constructor is generated.
// But the move constructor, copy assignment and move assignment operators are provided by compiler...
// same for copy assignment op is provided constructor is generated.
// Move constructor:
// if defined, copy constructor is deleted but copy assignment operator and
// move assignment operator are provided.