-
Notifications
You must be signed in to change notification settings - Fork 16
/
default_move_copy_constructor.cpp
65 lines (52 loc) · 1.06 KB
/
default_move_copy_constructor.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
#include <iostream>
//move constructor
//https://stackoverflow.com/a/25123890/10651567
/*
calling a function by value means we need to create a new object,
so copy constructor or move constructor will be called
*/
//https://stackoverflow.com/questions/16728441/why-copy-constructor-is-call-when-we-pass-an-object-as-an-argument-by-value-to-a
class A
{
public:
int a;
int b;
A(){
std::cout << "default ctor" << std::endl;
}
A(A&& other) : a{std::move(other.a)}, b{std::move(other.b)}{
std::cout << "move ctor" << std::endl;
}
A(A& other) : a{other.a}, b{other.b}{
std::cout << "copy ctor" << std::endl;
}
};
void f1(A *a){
};
void f2(A a){
};
void f3(A &a){
};
void f4(A&& a){
};
int main(){
A objA;
std::cout << "f1" << std::endl;
f1(&objA);
std::cout << "f2 - lvalue" << std::endl;
f2(objA);
std::cout << "f2 - rvalue" << std::endl;
f2(std::move(objA));
std::cout << "f3" << std::endl;
f3(objA);
return 0;
}
/*
default ctor
f1
f2 - lvalue
copy ctor
f2 - rvalue
move ctor
f3
*/