Why isn't there a consolidated Copy and Assignment constructor in C ++?

I understand the scenarios in which the corresponding functions will be called (Copy Constructor and Assignment operator). And both of these functions literally perform the same functionality - properly allocate memory for dynamic data items and copy data from the passed argument object so that both objects look the same in the data. Why not in this case, C ++ provides a consolidated (one function) that will be called in both of these scenarios instead of complicating things by providing two options?

+3


source to share


2 answers


They are not the same and it would be a pain in the neck if someone forced them to be.

Copying a design is a way to create an object. Base element initializers can be used, among other things. In multi-threaded code, you don't need to worry so much about mutual exclusion elements in the constructor, since you cannot create the same object at the same time!



The assignment operator does a very different matter. It works on an object that already exists and should return a reference to itself. An implementation can do slightly different things here cf. copy construction. For example, a row class might not allocate resources if the newly assigned row is smaller.

In simple cases, they can do the same, and the return value of the assignment is discarded. But in such cases, you can rely on the ones that the compiler automatically generates.

+5


source


They are not the same. They may be the same in special cases, but usually not.

when you have something like this:

std::vector myVec = myOtherVec;

      

Similar to the assignment, but the copy constructor is actually called.

The copy constructor starts an object from nothing .

This goes to the main question of what is the difference between malloc

(the old C way to reserve memory) and new

. The difference is this: it new

calls the constructor of your object, which is very important in C ++, otherwise we will talk about garbage memory, which cannot be initialized unless explicitly specified.

For example, internally std::vector

there is a variable size

that keeps track of the number of items actively confirmed by the user with push_back()

or resize

(we're not talking about reserve).



Now imagine how this is implemented:

template <typename T>
class vector
{
    int size;
    T* theArray;
    void reserveMyMemory(); //ignoring allocators for simplicity
}

      

What is the difference between a copy constructor and an assignment operator?

  • Assignment operator: Simply copies the size and contents of the array.
  • Copy-cosntructor: It is necessary to reserve memory and initialize variables and then copy.

Now the image that requires memory reservation needs to be checked size

and theArray

nullptr

. What happens if it uses an assignment operator internally? Catastrophe. Since the values ​​are not initialized. So, you need to create a constructor.

In this case, the copy constructor is more general in that it must initialize the variables and then copy the elements that it must copy. Of course, this whole example is just a demonstration. Don't take it literally std::vector

, STL doesn't work that way.

+3


source







All Articles