Skip to content

Instantly share code, notes, and snippets.

@ajaynitt
Last active August 29, 2015 14:07
Show Gist options
  • Save ajaynitt/ab6a7fc85b07f1f7c55e to your computer and use it in GitHub Desktop.
Save ajaynitt/ab6a7fc85b07f1f7c55e to your computer and use it in GitHub Desktop.
Copy All parts of an Object
//Item 12 from effective c++
//helper function to make a log entry
void logCall(const std::string & name);//make a log entry
//base class
class Customer
{
public:
Customer(const Customer & rhs);
Customer& operator = (const Customer & rhs);
private:
std::string name;
};
//derived class
class PriorityCustomer:public Customer
{
public:
PriorityCustomer(const PriorityCustomer & rhs);
priotityCustomer & operator = (const PriorityCustomer & rhs);
private:
int priority;
}
@ajaynitt
Copy link
Author

Rememebre:
When you are writing a copying function(copy ctor + assignment operator) ,be sure to
(1)Copy all the local data members
(2)invoke the appropriate copying functions in all the base classes too.

@ajaynitt
Copy link
Author

Note:we can create a function init() as private data members and avoid code duplication between copy ctor and assignment copy ctor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment