Possible Duplicate:
What is The Rule of Three?
When you require to define to your own assignment operator?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally, you’ll need to define your own assignment operator under the same circumstances when you need to define your own copy constructor – i.e. when a default copy won’t cut it. This happens in cases when your object manages dynamically allocated memory or other resources which need to be specially copied.
For example, if you have a class which manages a pointer that points to dynamically allocated memory, the default assignment operator will simply copy the pointer. Generally, this is not what you want – you want each object instance to have its own internal copy of the allocated data, and so you’ll need a special assignment operator that allocates its own memory and performs a copy. This is, for example, what
std::vectorneeds to do when copied or assigned.