Monthly Archives: September 2009

Implementing assignment operator using copy constructor

Many times we are required to define both a copy constructor and an assignment operator (for example: according to the rule of three, if we need one we are likely to need the other). The two will probably share a big portion of their code, if not all of it. The most common solution is to export the code to a private method which will be called from both the constructor and the assignment operator, effectively giving up on initialization list benefits in the constructor, or duplicating code.

A pretty nice way to overcome this is by implementing the assignment operator in terms of the copy constructor. However, I will explain later why this suggested way is not really recommended to use.

Continue reading Implementing assignment operator using copy constructor