The big 3-Copy C'tor, D'tor, Ass.op

Hi guys,
I was wondering about a statement I was told during my OOP lecture:
"If you need one of them(Copy C'tor, D'tor, Ass.op)- then you need them all",
Why is that?

Cheers
 
// 
closed account (3qX21hU5)
If your class has something in it that makes it so that the implicitly generated copy constructor (Compiler generated) won't work how you want it to. Then that means that the implicit assignment operator and implicit destructor most likely won't work how you want them to either. So that is why you will generally need them all.

One reason I run into a lot why the implicit copy constructor (Or the other two) won't work is when you have a raw pointer in your class as a member (Like int* myPointerMember;). The copy constructor won't work because it won't copy the pointer how you want it to most likely, the assignment operator will work the same as the copy constructor and the destructor won't work correctly because of the pointer.

Now I know that doesn't explain much but don't really have time to type out a example for you sorry :(. Though I am sure another member will be more then willing to help explain it more. You can also check out the wiki on it http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) and this SO questions gives some pretty good answers http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three
Last edited on
We use these when we Have pointers reference Dynamically allocated memory.
When we want to assign One object to another The copy constructors invoked if the object has not been created. ex:

Person p;
Person c = p;// Copy constructor used

Two objects are already in existence we use the overloaded assignment operator.

Person p(1, 2);
Person c;

c=p; // Overloaded assignment operator used

The destructors Are used to clean up dynamically Allocated memory

Also forgot to mention that we want Our objects to use their own Copies of those variables.
Topic archived. No new replies allowed.