-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMove_constructors.cpp
More file actions
54 lines (48 loc) · 1.48 KB
/
Move_constructors.cpp
File metadata and controls
54 lines (48 loc) · 1.48 KB
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
#include <vector>
#include <iostream>
class Base
{
private:
std::vector<int> _data;
public:
Base(const int& d1, const int& d2, const int d3);
~Base(void);
Base(const Base& other) = delete;
Base& operator=(const Base& other) = delete;
Base(Base&& other) noexcept;
Base& operator=(Base&& other) noexcept = delete;
private:
void Print(void) const {
std::cout << "[" << _data.size() << "]" << "\t";
for (const int& d : _data) std::cout << d << "\t";
}
};
Base::Base(const int& d1, const int& d2, const int d3) {
_data.push_back(d1);
_data.push_back(d2);
_data.push_back(d3);
std::cout << "Base constructor: ";
this->Print(); std::cout << std::endl;
}
Base::~Base(void) {
std::cout << "Base destructor: ";
this->Print(); std::cout << std::endl;
}
// Move
Base::Base(Base&& other) noexcept {
std::cout << "Base move constructor: " << std::endl;
std::cout << "Before: "; other.Print(); this->Print(); std::cout << std::endl;
std::swap(_data, other._data);
std::cout << "After: "; other.Print(); this->Print(); std::cout << std::endl;
}
int main(int argc, char** argv)
{
std::cout << "-----------------------------------" << std::endl;
Base base1(1, 2, 3);
std::cout << "-----------------------------------" << std::endl;
Base base2(4, 5, 6);
std::cout << "-----------------------------------" << std::endl;
std::vector<Base> bases;
bases.push_back(Base(7, 8, 9));
std::cout << "-----------------------------------" << std::endl;
}