-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_copy_constructor.cpp
49 lines (41 loc) · 1.06 KB
/
vector_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
#include <iostream>
#include <vector>
using namespace std;
class Move{
private:
int *data;
public:
Move(int d)
{
data = new int;
*data = d;
cout << "Constructor is called for " << d << endl;
}
//copy constructor
Move(const Move& source)
: Move{*source.data}
{
cout << "Copy constructor is called - deep copy for "<< *source.data << endl;
}
// Move Constructor
Move(Move&& source)
: data{ source.data }
{
cout << "Move Constructor for "<< *source.data << endl;
source.data = nullptr;
}
~Move(){
if (data != nullptr)
cout << "destructor called for " << *data << endl;
else
cout << "destructor called for nullptr" << endl;
delete data;
}
};
int main(){
vector<Move> vec; //creating a vector of move class
//inserting object of move class
vec.push_back(Move{40});
vec.push_back(Move{90});
return 0;
}