forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13_08.h
40 lines (35 loc) · 829 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
34
35
36
37
38
39
40
//
// ex13_08.h
// CP5
//
// Created by pezy on 1/12/15.
// Copyright (c) 2015 pezy. All rights reserved.
//
// 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