简体   繁体   中英

Why not a consolidated Copy constructor and Assignment operator available in C++?

I do understand the scenarios where the respective functions (Copy Constructor and Assignment operator) would be called. And both these functions are literally doing the same functionality - properly allocating memory for dynamic data members and copy the data from the passed argument object, so that both the object looks identical in data. Why not in that case, C++ provides a consolidated (one function) which would be called in both these scenarios instead of complicating things by providing two variants?

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

Copy construction is a way of creating an object. Amongst other things, base member initialisers can be used. In multi-threaded code, you don't need to worry so much about mutual exclusion units in a constructor since you cannot create the same object simultaneously!

An assignment operator does a rather different thing. It operates on an object that already exists and should return a reference to self . An implementation can do subtly different things here cf. copy construction. For example, a string class might not release resources if the new assigned string is smaller.

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

They are so not the same. They might be the same in special cases, but generally, no.

when you have something like this:

std::vector myVec = myOtherVec;

It looks like assignment, but actually the copy constructor is being called.

The copy constructor starts an object from nothing .

This falls back to the basic question of what's the difference between malloc (the old C way to reserve memory) and new . The difference is: new calls the constructor of your object, which is very important in C++, otherwise we'd be talking about garbage memory that can't be initialzed unless it explicitly is.

For example, in the internal implementation of std::vector , there is a size variable that tracks the number of elements actively acknowledged by the user with push_back() or resize (we're not talking about reserve).

Now imagine how it's implemented:

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

What's the difference between the copy constructor and assignment operator?

  • Assignment operator: Just copies size and the array content.
  • Copy-cosntructor: Must reserve memory and initialize variables, then copy.

Now imageine that memory reserving requires checking the size and whether theArray is nullptr . What's going to happen if internally it uses assignment operator? A catastrophe. Because the values are not initialized. So, you need a constructor to start.

In this case, the copy constructor is more general, because it should initialize variables , then copy the elements it has to copy. Of course, this whole example is just a demonstration. Don't take it literally for std::vector , the STL doesn't work that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM