Repository navigation
Expand file tree
/
Copy pathIntCellPtrOK.cpp
More file actions
75 lines (66 loc) · 1.45 KB
/
Copy pathIntCellPtrOK.cpp
File metadata and controls
75 lines (66 loc) · 1.45 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "IntCellPtrOK.h"
/**
* @brief Default Constructor
* @param initialValue initial int value
*/
IntCellPtrOK::IntCellPtrOK(int initialValue)
{
storedValue = new int{initialValue};
// Note [] and {} are different!
// same as: storedValue = new int; *storedValue = initialValue;
}
/**
* @brief Rule-of-three: 1. Copy Constructor
*
* @param rhs
*/
IntCellPtrOK::IntCellPtrOK(const IntCellPtrOK &rhs)
{
storedValue = new int{*(rhs.storedValue)};
// same as: storedValue = new int; *storedValue = *(rhs.storedValue);
// or: storedValue = new int; write(rhs.read());
}
/**
* @brief Rule-of-three: 2. Copy assignment operator
*
* Remember:
* 1. Operators are called for the lhs of the assignment which is the *this object.
* 2. For chained assignments operator= always returns *this as a reference.
*
* @param rhs
* @return IntCellPtrOK&
*/
IntCellPtrOK &IntCellPtrOK::operator=(const IntCellPtrOK &rhs)
{
// check for self-assignment first
if (this == &rhs)
return *this;
// make a deep copy of the rhs
*storedValue = *(rhs.storedValue);
return *this;
}
/**
* @brief Rule-of-three: 3. Destructor
*/
IntCellPtrOK::~IntCellPtrOK()
{
delete storedValue;
}
/**
* @brief read the int value
*
* @return int
*/
int IntCellPtrOK::read() const
{
return *storedValue;
}
/**
* @brief store the int value.
*
* @param x the int value
*/
void IntCellPtrOK::write(int x)
{
*storedValue = x;
}