forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13_08.h
33 lines (29 loc) · 760 Bytes
/
ex13_08.h
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
//
// ex13_08.h
// CP5
//
// Created by pezy on 1/12/15.
//
// Write the assignment operator for the HasPtr class from exercise 13.5 in 13.1.1 (p. 499).
// As with the copy constructor, your assignment operator should copy the object to which ps points.
//
// See ex13_05.h
#ifndef CP5_ex13_08_h
#define CP5_ex13_08_h
#include <string>
class HasPtr {
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { }
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
HasPtr& operator=(const HasPtr &hp) {
std::string *new_ps = new std::string(*hp.ps);
delete ps;
ps = new_ps;
i = hp.i;
return *this;
}
private:
std::string *ps;
int i;
};
#endif