From ba81d275b4ede69d75bf1b98576f040b493106cb Mon Sep 17 00:00:00 2001 From: Soyn Date: Sat, 30 Apr 2016 17:24:31 +0800 Subject: [PATCH 1/3] add the exception safety solution for copy-assignment operator --- ch13/ex13_08.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ch13/ex13_08.h b/ch13/ex13_08.h index 9192c807..70beb2ed 100644 --- a/ch13/ex13_08.h +++ b/ch13/ex13_08.h @@ -7,7 +7,7 @@ // 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 +// See ex13_05.h #ifndef CP5_ex13_08_h #define CP5_ex13_08_h @@ -18,11 +18,18 @@ 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; + 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; + ps = temp_ps; + + i = temp_HasPtr.i; + } // temp_HasPtr's destructor will be called, + // the memory allocated by original instance will be freed. return *this; } private: From b4a8cf17903099ca39b270bea374f10fe523d9ee Mon Sep 17 00:00:00 2001 From: Soyn Date: Tue, 3 May 2016 15:45:58 +0800 Subject: [PATCH 2/3] fix the solution --- ch13/ex13_08.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ch13/ex13_08.h b/ch13/ex13_08.h index 70beb2ed..59cf76c4 100644 --- a/ch13/ex13_08.h +++ b/ch13/ex13_08.h @@ -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: From 3f6de3125149891ac66268138ce4f025160feddd Mon Sep 17 00:00:00 2001 From: Soyn Date: Tue, 3 May 2016 16:12:52 +0800 Subject: [PATCH 3/3] fix --- ch13/ex13_08.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13/ex13_08.h b/ch13/ex13_08.h index 59cf76c4..1f01fdad 100644 --- a/ch13/ex13_08.h +++ b/ch13/ex13_08.h @@ -20,7 +20,7 @@ class HasPtr { HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } HasPtr& operator=(const HasPtr &rhs_hp) { if(this != &rhs_hp){ - std :: string *temp_ps = new std :: string(*rhs_hp); + std::string *temp_ps = new std::string(*rhs_hp.ps); delete ps; ps = temp_ps; i = rhs_hp.i;