forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex13.5.cpp
42 lines (34 loc) · 1006 Bytes
/
ex13.5.cpp
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
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 30 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 13.5:
//! Given the following sketch of a class, write a copy constructor that copies
//! all the members. Your constructor should dynamically allocate a new string
//! (§ 12.1.2, p. 458) and copy the object to which ps points, rather than copying
//! ps itself.
//!
#include <string>
#include <iostream>
class HasPtr
{
public:
HasPtr(const std::string &s = std::string()):
ps(new std::string(s)), i(0) { }
/**
* ex13.5, this exercise asks for a deep copy.
* Hence new dynamic memory must be allocated .
*/
HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { }
//! ^^^^^^^^^^^^^^^
private:
std::string *ps;
int i;
};
int main()
{
return 0;
}