Skip to content

Instantly share code, notes, and snippets.

@rootid
Created June 6, 2015 22:01
Show Gist options
  • Save rootid/929e4b14fa71199c944c to your computer and use it in GitHub Desktop.
Save rootid/929e4b14fa71199c944c to your computer and use it in GitHub Desktop.
Generic copy ctor in c++
//Copy-Constructor and assignment operator
MyClass::MyClass() : /* Fill in initializer list. */ {
/* Default initialization here. */
}
//copy constructor
MyClass::MyClass(const MyClass& other) {
copyOther(other);
}
//assignmet-operator
MyClass& MyClass::operator =(const MyClass& other) {
if(this != &other) {
clear();
// Note: When we cover inheritance, there's one more step here.
copyOther(other);
}
return *this;
}
MyClass::~MyClass() {
clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment