difference between copy constructor and assignment operator

Deadly 提交于 2021-02-11 14:21:47

问题


I have gone through [question] (What's the difference between assignment operator and copy constructor?) and understood difference between a copy constructor and assignment operator. Now my question is although copy constructor initializes the previously uninitialized object where as assignment operator replaces data from previously initialized object what's the difference in terms of final outcome. I am thinking that final outcome in both case comes out to be same right? In the end after copying via (CC) we get the same output and even after using (AO) we get the same output. Am I making sense here? Can someone please clarify what is the difference in terms of real world application?


回答1:


Other than what you said, there is no difference.
The CC works on new unitizialied objects (it´s a constructor after all), the operator on existing ones.
The use of the CC could be replaced with the normal constructor and then the assignment op (in usual classes), but this would be not as efficient as the direct construction with copied data.
Eg.

class C
{
private:
    vector<int> v;
public
    C()
    {
        //fill v with 10^9 slowly generated random numbers, takes ca. 2 days
    }
    C(const C& c)  //could be auto-generated in this case
    {
        v = c.v;
    }
    C &operator=(const C& c)  //could be auto-generated in this case
    {
        v = c.v;
        return *this;
    }
};

...

C oldc;

...

//either
C newc(oldc);
//or
C newc; //takes 2 days
newc = oldc;

Another reason, some nontrivial classes have no (public) default constructor and can only be created by copying existing objects from somewhere.




回答2:


The final outcome depends entirely on the situation. You just need to understand that

  1. Copy constructor is called when a new object is created from an existing object, as a copy of the existing object.

  2. Assignment operator is called when both the objects are already created and we are assigning one to other. ex: a = b

Difference:

  • Copy constructor creates a new object which has the copy of the original object .

  • On the other hand assignment operators does not create any new object. It instead, deals with existing objects.

For pointer copying we need Deep copy as compiler's default copy constructor and assignment operator does Shallow copy . So Check this out for deeper understanding Shallow copy vs Deep copy




回答3:


It depends on you. The language makes no requirements with regards to what the copy constructor or assignment actually do. Of course, any clients will expect that the observable state of the object be the same as that of the copied object, in both cases; this includes code in the standard library which may copy or assign to your object types. But there are legitimate cases where the class might contain meta information, used for instrumentation or resource management, which could be different in the two cases. (It's also frequent for a class to support copy construction, for use in a clone function, but not to support assignment.)

Of course, the implementation may be very different; if the object contains a pointer to dynamically allocated memory, for example:

class Toto
{
    Titi* myPtr;
public:
    Toto() : myPtr( new Titi ) {}
    Toto( Toto const& other ) : myPtr( other.myPtr->clone()) {}
    ~Toto() { delete myPtr; }
    Toto& operator=( Toto const& other )
    {
        Titi* tmp = other.myPtr->clone();
        delete myPtr;
        myPtr = tmp;
        return *this;
    }
};

(In such a simplified case as this, of course, you'd not use a pointer, and just have the class contain an instance of Titi. In a more complicated case, where Titi is in fact an abstract base class, and you instantiate different derived classes according to some arguments to the constructors, something like this may be justified, however.)




回答4:


In copy constructor you are creating new object in assignment you are assigning value to the existing object from other object of same time. So the output may be different the way you implement copy constructor and assignment operator functions




回答5:


I'm sure you already found dozens of posts regarding the difference between a copy constructor and an assignment operator, or at least their default behavior (if you overload them, there's no limit to what you can do...or break). So I'll limit my answer to your specific question about the final outcome.

The outcome of calling either of them is that you end up with initialized (or reinitialized) data or object... However there is more to it than that because they are called in different circumstances and affect memory differently. Without going into long discussions about possible unwanted behaviors, stack usage, shallow vs deep copy (you need to be careful about this when dealing with pointers), the copy constructor will create a new instance of an object and copy data to it, while the assignment operator works with and already existing copy.

I hope this answers your question...



来源:https://stackoverflow.com/questions/27796197/difference-between-copy-constructor-and-assignment-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!