Skip to content

Commit

Permalink
fix the solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Soyn committed May 3, 2016
1 parent ba81d27 commit b4a8cf1
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions ch13/ex13_08.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ class HasPtr {
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 &rhs_hp) {
if(this != &rhs_hp){ // there is no need to assign for
// the same instance.

HasPtr temp_HasPtr(rhs_hp);
std :: string *temp_ps = temp_HasPtr.ps;
temp_HasPtr.ps = ps;
if(this != &rhs_hp){
std :: string *temp_ps = new std :: string(*rhs_hp);
delete ps;
ps = temp_ps;

i = temp_HasPtr.i;
} // temp_HasPtr's destructor will be called,
// the memory allocated by original instance will be freed.
i = rhs_hp.i;
}
return *this;
}
private:
Expand Down

0 comments on commit b4a8cf1

Please sign in to comment.