Last active
August 29, 2015 14:01
-
-
Save tejainece/1f42daf6f77729a58b7d to your computer and use it in GitHub Desktop.
C++ copy constructor and assignment operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Assignable { | |
| private: | |
| int vPrivate = 0; | |
| public: | |
| int vPublic = 0; | |
| Assignable() { | |
| vPrivate = 0; | |
| vPublic = 0; | |
| cout << "Constructed()" << endl; | |
| } | |
| explicit Assignable(int _public) : vPublic(_public) { | |
| cout << "Constructed(int)" << endl; | |
| } | |
| void setPrivate(int _private) { | |
| vPrivate = _private; | |
| } | |
| void print() const { | |
| cout << vPublic << " " << vPrivate << endl; | |
| } | |
| Assignable& operator=( const Assignable& other ) { | |
| cout << "Called assignment operator" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| return *this; | |
| } | |
| Assignable(const Assignable &other) { | |
| cout << "called copy constructor" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| } | |
| ~Assignable() { | |
| cout << "destructed" << endl; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| a2 = a1; | |
| a2.print(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cout << "Copy constructor call demo: when an instance of a class is passed " | |
| "as value to a function" << endl; | |
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| printAssignable(a1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cout << "Copy constructor call demo: when an instance of a class is " | |
| "returned by value " << endl; | |
| Assignable a1 = giveMeNewClassNoopt(55, 55); | |
| a1.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a2 = giveMeNewClassOptimized(55, 55); | |
| a2.print(); | |
| cout << "Don't be surprised if copy constructor is not called here. " | |
| "Compilers are known to optimize away this copy" << endl; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cout << "Copy constructor call demo: when a new instance of a class is " | |
| "created from an existing object" << endl; | |
| Assignable a1(5); | |
| a1.setPrivate(5); | |
| Assignable a2(a1); | |
| a2.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a3 = a1; | |
| a3.print(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class Assignable { | |
| private: | |
| int vPrivate = 0; | |
| public: | |
| int vPublic = 0; | |
| Assignable() { | |
| vPrivate = 0; | |
| vPublic = 0; | |
| cout << "Constructed()" << endl; | |
| } | |
| explicit Assignable(int _public) : vPublic(_public) { | |
| cout << "Constructed(int)" << endl; | |
| } | |
| void setPrivate(int _private) { | |
| vPrivate = _private; | |
| } | |
| void print() const { | |
| cout << vPublic << " " << vPrivate << endl; | |
| } | |
| Assignable& operator=( const Assignable& other ) { | |
| cout << "Called assignment operator" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| return *this; | |
| } | |
| Assignable(const Assignable &other) { | |
| cout << "called copy constructor" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| } | |
| ~Assignable() { | |
| cout << "destructed" << endl; | |
| } | |
| }; | |
| Assignable giveMeNewClassOptimized(int vPublic, int vPrivate) { | |
| Assignable nClass(vPublic); | |
| nClass.setPrivate(vPrivate); | |
| return nClass; | |
| } | |
| Assignable giveMeNewClassNoopt(int vPublic, int vPrivate) { | |
| Assignable *nClass = new Assignable(vPublic); | |
| nClass->setPrivate(vPrivate); | |
| return *nClass; | |
| } | |
| void printAssignable(Assignable a) { | |
| a.print(); | |
| } | |
| int main() | |
| { | |
| cout << "Hello World" << endl; | |
| int i = 2; | |
| if(i == 0) { | |
| cout << "Copy constructor call demo: when an instance of a class is passed " | |
| "as value to a function" << endl; | |
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| printAssignable(a1); | |
| } else if(i == 1) { | |
| cout << "Copy constructor call demo: when a new instance of a class is " | |
| "created from an existing object" << endl; | |
| Assignable a1(5); | |
| a1.setPrivate(5); | |
| Assignable a2(a1); | |
| a2.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a3 = a1; | |
| a3.print(); | |
| } else if(i == 2) { | |
| cout << "Copy constructor call demo: when an instance of a class is " | |
| "returned by value " << endl; | |
| Assignable a1 = giveMeNewClassNoopt(55, 55); | |
| a1.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a2 = giveMeNewClassOptimized(55, 55); | |
| a2.print(); | |
| cout << "Don't be surprised if copy constructor is not called here. " | |
| "Compilers are known to optimize away this copy" << endl; | |
| } else if(i == 3) { | |
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| a2 = a1; | |
| a2.print(); | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class Assignable { | |
| private: | |
| int vPrivate = 0; | |
| public: | |
| int vPublic = 0; | |
| Assignable() { | |
| vPrivate = 0; | |
| vPublic = 0; | |
| cout << "Constructed()" << endl; | |
| } | |
| explicit Assignable(int _public) : vPublic(_public) { | |
| cout << "Constructed(int)" << endl; | |
| } | |
| void setPrivate(int _private) { | |
| vPrivate = _private; | |
| } | |
| void print() const { | |
| cout << vPublic << " " << vPrivate << endl; | |
| } | |
| Assignable& operator=( const Assignable& other ) { | |
| cout << "Called assignment operator" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| return *this; | |
| } | |
| Assignable(const Assignable &other) { | |
| cout << "called copy constructor" << endl; | |
| this->vPublic = other.vPublic; | |
| this->vPrivate = other.vPrivate; | |
| } | |
| ~Assignable() { | |
| cout << "destructed" << endl; | |
| } | |
| }; | |
| Assignable giveMeNewClassOptimized(int vPublic, int vPrivate) { | |
| Assignable nClass(vPublic); | |
| nClass.setPrivate(vPrivate); | |
| return nClass; | |
| } | |
| Assignable giveMeNewClassNoopt(int vPublic, int vPrivate) { | |
| Assignable *nClass = new Assignable(vPublic); | |
| nClass->setPrivate(vPrivate); | |
| return *nClass; | |
| } | |
| void printAssignable(Assignable a) { | |
| a.print(); | |
| } | |
| int main() | |
| { | |
| cout << "Hello World" << endl; | |
| int i = 2; | |
| if(i == 0) { | |
| cout << "Copy constructor call demo: when an instance of a class is passed " | |
| "as value to a function" << endl; | |
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| printAssignable(a1); | |
| } else if(i == 1) { | |
| cout << "Copy constructor call demo: when a new instance of a class is " | |
| "created from an existing object" << endl; | |
| Assignable a1(5); | |
| a1.setPrivate(5); | |
| Assignable a2(a1); | |
| a2.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a3 = a1; | |
| a3.print(); | |
| } else if(i == 2) { | |
| cout << "Copy constructor call demo: when an instance of a class is " | |
| "returned by value " << endl; | |
| Assignable a1 = giveMeNewClassNoopt(55, 55); | |
| a1.print(); | |
| cout << endl << "Another flavor:" << endl; | |
| Assignable a2 = giveMeNewClassOptimized(55, 55); | |
| a2.print(); | |
| cout << "Don't be surprised if copy constructor is not called here. " | |
| "Compilers are known to optimize away this copy" << endl; | |
| } else if(i == 3) { | |
| Assignable a1(5); | |
| Assignable a2(10); | |
| a1.setPrivate(5); | |
| a2.setPrivate(10); | |
| a2 = a1; | |
| a2.print(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment