开发者

Why assignment operator is used for deep copy and who calls it

During deep copy we are writing overloaded copy constructor and assignment operator. May i know why we have to write the overloaded assignment operator because we doing the same in overloaded copy constructor(except some check and return this).

开发者_StackOverflow中文版

Who is calling assignment operator


Follow the Rule of Three:
If you need to write an copy constructor for your class, You also should write the Copy assignment operator and Destructor.

Copy Assignment operator and Copy Constructor both are called Copying functions. They basically help in getting a new copy of an object from an existing object. They both are separate entities invoked in different scenarios. So, just as in case of Copy constructor you ensure that you make deep copies of all pointer members and not just shallow copies, same would applies for copy assignment operator.

An Code Example:

class MyClass obj1, obj2;
class MyClass obj3(obj1);     //Calls Copy Constructor
obj1 = obj2;                  //Calls Copy Assignment Operator


The assignment operator is used if you do this:

MyType my1, my2;
my1 = my2;  // same as: my1.operator=(my2);


The copy constructor and the assignment operator usually have very similar code, but if done properly (initializer lists) should be coded differently, used differently, and perform differently.

The copy constructor should use initializer lists. This is used for creating a new vector object that is the same as one already existing.

vector::vector(const vector& b) 
    :size(b.size),
    capacity(b.capacity),
    data(new TYPE[size]) 
{
    //there should be minimal code here
    //I'm skipping copying the data, because doing it right
    //is hard and beside the point
} 

vector seven;
seven.push_back(7);
vector seven_copy(seven); //make a new one, same as old

The assignment operator is probably exactly what you have. This is used to reassign an already existing vector object to be the same as one already existing

vector& vector::operator=(const vector& b) 
{
    //carefully written so self assignment doesn't crash.  
    TYPE* temp = new TYPE[b.size];
    //I'm skipping copying the data, because doing it right
    //is hard and beside the point
    delete [] data;
    //all exceptions that can be thrown, have, so it's safe to modify members now
    data = temp;
    size = b.size;
    capacity = b.capacity;
    return *this;
}

vector nine;
nine.push_back(9);
nine = seven;  //make an old one the same as another old

It should be noted that the move constructor and move assignment may look vaguely similar, but should probably be different as well.

vector::vector(vector&& b) 
    :size(b.size)
    capacity(b.capacity)
    data(b.data) //different!
{
    b.data = nullptr; 
}
vector& operator=(vector&& b)
{
     //since b.data is tied to b.size and b.capacity, it's safest to just swap
     //so if someone calls b.push_back() after, it's all still safe.
     std::swap(size, b.size);
     std::swap(capacity, b.capacity);
     std::data(data, b.data);
     return *this;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜