forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hasptr.h
54 lines (44 loc) · 1.12 KB
/
hasptr.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/***************************************************************************
* @file hasptr.h
* @author Alan.W
* @date 05 JAN 2014
* @remark move constructor added 09 Jan 2014
***************************************************************************/
#ifndef HASPTR_H
#define HASPTR_H
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
//! revised for ex13.31
//! a class holding a std::string*
class HasPtr
{
friend void swap(HasPtr&, HasPtr&);
friend bool operator <(const HasPtr& lhs, const HasPtr& rhs);
public:
//! default constructor.
HasPtr(const std::string &s = std::string()):
ps(new std::string(s)), i(0)
{ }
//! copy constructor.
HasPtr(const HasPtr& hp) :
ps(new std::string(*hp.ps)), i(hp.i)
{ }
//! move constructor.
HasPtr(HasPtr&& hp) noexcept :
ps(hp.ps), i(hp.i)
{ hp.ps = nullptr; }
HasPtr&
operator = (HasPtr rhs);
//! ^^ no const here
//! destructor.
~HasPtr()
{
delete ps;
}
private:
std::string *ps;
int i;
};
#endif // HASPTR_H