Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualiMalc

Posted 09 February 2013 - 01:25 PM

Do not ever implement copy construction in terms of assignment. It must be the other way around.
The reason is that the copy-construction only has 1 job to do which is to copy the object, but asignment has two jobs to do - copy the object and free the current state. When using the assignment operator inside the copy-constructor, it will try and free something, but what is it freeing since the object hasn't been initialisaed yet?! That can cause problems.

Also, your assignment operators should take their argument by const reference. You should not be trying to modify the item you are copying from. That will cause problems.
Use the copy and swap idiom whenever you can, but use it correctly!

Those warnings are definitely things to sort out.

#2iMalc

Posted 09 February 2013 - 01:23 PM

Do not ever implement copy construction in terms of assignment. It must be the other way around.
The reason is that the copy-construction only has 1 job to do which is to copy the object, but asignment has two jobs to do - copy the object and free the current state. When using the assignment operator inside the copy-constructor, it will try and free something, but what is it freeing? That can cause problems.
Use the copy and swap idiom whenever you can.

Also, your assignment operators should take their argument by const reference. You should not be trying to modify the item you are copying from. That will cause problems.

#1iMalc

Posted 09 February 2013 - 01:00 PM

Do not every implement copy construction in terms of assignment. It must be the other way around.
The reason is that the copy-construction only has 1 job to do which is to copy the object, but asignment has two jobs to do - copy the object and free the current state. When using the assignment operator inside the copy-constructor, it will try and free something, but what is it freeing? That can cause problems.
Use the copy and swap idiom whenever you can.

Also, your assignment operators should take their argument by const reference. You should not be trying to modify the item you are copying from. That will cause problems.

PARTNERS