Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the copy constructor in terms of operator=

Tags:

If the operator= is properly defined, is it OK to use the following as copy constructor?

MyClass::MyClass(MyClass const &_copy) {     *this = _copy; } 
like image 244
gregseth Avatar asked Sep 06 '10 14:09

gregseth


People also ask

How do you implement copy assignment operator in C++?

The implicitly declared copy assignment operator of a class A will have the form A& A::operator=(const A&) if the following statements are true: A direct or virtual base B of class A has a copy assignment operator whose parameter is of type const B& , const volatile B& , or B .

How do you create a copy constructor in C++?

Copy Constructor in C++ClassName (const ClassName &old_obj); Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

What is copy constructor in op?

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to − Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.


1 Answers

If all members of MyClass have a default constructor, yes.

Note that usually it is the other way around:

class MyClass { public:     MyClass(MyClass const&);     // Implemented     void swap(MyClass&) throw(); // Implemented     MyClass& operator=(MyClass rhs) { rhs.swap(*this); return *this; } }; 

We pass by value in operator= so that the copy constructor gets called. Note that everything is exception safe, since swap is guaranteed not to throw (you have to ensure this in your implementation).

EDIT, as requested, about the call-by-value stuff: The operator= could be written as

MyClass& MyClass::operator=(MyClass const& rhs) {     MyClass tmp(rhs);     tmp.swap(*this);     return *this; } 

C++ students are usually told to pass class instances by reference because the copy constructor gets called if they are passed by value. In our case, we have to copy rhs anyway, so passing by value is fine.

Thus, the operator= (first version, call by value) reads:

  • Make a copy of rhs (via the copy constructor, automatically called)
  • Swap its contents with *this
  • Return *this and let rhs (which contains the old value) be destroyed at method exit.

Now, we have an extra bonus with this call-by-value. If the object being passed to operator= (or any function which gets its arguments by value) is a temporary object, the compiler can (and usually does) make no copy at all. This is called copy elision.

Therefore, if rhs is temporary, no copy is made. We are left with:

  • Swap this and rhs contents
  • Destroy rhs

So passing by value is in this case more efficient than passing by reference.

like image 92
Alexandre C. Avatar answered Oct 14 '22 10:10

Alexandre C.