Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in which case we need to disable default copy constructor and assign operator?

If we put copy constructor and assign operator as private and provide no implementation, they will be disabled, like this:

class Test
{
   private:
       Test(const Test&);
       Test& operator=(const Test&);
};

but in which case we need to do this? I mean when should we do it like this?

like image 892
ratzip Avatar asked Mar 18 '15 11:03

ratzip


People also ask

In what situations a copy constructor is invoked instead of assignment operator?

A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object.

Why copy constructor and assignment operator's working is different?

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.

Why is copy assignment operator needed?

Both the copy constructors and assignment operators copies from one object to another then why do we need both? Copyconstructor means it creates a new object and copies the contents from another exist object and assignment operator copies the contents from one existing object to the already existing object.

How do I turn off assignment operator?

A cleaner solution: create a class where the default copy constructor and/or default copy assignment operator are deleted. Derive all your classes or the base class of the class hierarchies from this special class. Copy construction and/or copy assignment will now be disabled for all these classes automatically.


2 Answers

When you want the objects of this class to be non-copyable.

There may be many reasons when objects can't be or shouldn't be copied to other objects. A few examples are:

  1. Log files
  2. Some mutices
  3. In Singleton pattern
  4. Object Factory
  5. Some versions of smart pointers

For above examples, compiler provided version of default copy constructor and default assignment operators may lead to unexpected results.

c++11 onwarnds, you can use =delete syntax to delete the compiler provided default versions.

Another use is to force (restrict) copying of objects only via class utilities virtual Base* clone() for example.

Related: Rule of three or rule of five

like image 102
Mohit Jain Avatar answered Oct 02 '22 14:10

Mohit Jain


Another common case is when the object is not shallow-copyable and the one who wrote the class doesn't want to implement deep copy for it, so he left those two disabled, with or without comments on the rationale.

For example, when constructor news an array and destructor deletes it, then the object not shallow-copyable. To implement deep copy, copy constructor needs to new an empty array to the same size and then copy each and every element, and copy-assignment has to delete the old one before doing the same thing.

It's a lot of boilerplate code to write. And it takes time to run, too.

So if this class is private to an implementation and copy-constructor and copy-assignment are neither mandatory nor helpful, why not just disable them?

like image 28
user3528438 Avatar answered Oct 02 '22 13:10

user3528438