blob: ae782801cea0e761632825214f034b9f86b05960 (
plain) (
blame)
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
|
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person() : Person("Unknown", 0)
{
}
Person::Person(const char* name, int age) : name(nullptr), age(age)
{
cout << "Constr" << endl;
setName(name);
setAge(age);
}
Person::Person(const Person& other)
{
cout << "copy constr" << endl;
copyFrom(other);
}
Person::Person(Person&& other)
{
cout << "Move constr" << endl;
name = other.name;
age = other.age;
other.name = nullptr;
}
Person& Person::operator=(Person&& other)
{
std::cout << "Move op= " << endl;
if (this != &other)
{
free();
name = other.name;
other.name = nullptr;
age = other.age;
}
return *this;
}
Person& Person::operator = (const Person& other)
{
cout << "op =" << endl;
if (this != &other)
{
free();
copyFrom(other);
}
return *this;
}
Person::~Person()
{
cout << "Destructor" << endl;
free();
}
const char* Person::getName() const
{
return name;
}
int Person::getAge() const
{
return age;
}
void Person::setName(const char* name)
{
delete[] this->name;
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
void Person::setAge(int age)
{
if (age < 0 || age > 120)
age = 40;
this->age = age;
}
void Person::copyFrom(const Person& other)
{
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
age = other.age;
}
void Person::free()
{
delete[] name;
age = 0;
}
|