简体   繁体   English

为什么将赋值运算符用于深层复制以及由谁调用

[英]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). 我可能知道为什么我们必须编写重载的赋值运算符,因为我们在重载的副本构造函数中进行了相同的操作(除了一些检查并返回此值)。

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赋值运算符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. 因此,就像在使用Copy构造函数的情况下一样,请确保对所有指针成员进行深拷贝,而不仅仅是浅拷贝,复制赋值运算符也是如此。

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. 应当注意,move构造函数和move分配可能看起来模糊不清,但可能也应该有所不同。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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