简体   繁体   English

为什么C ++中没有合并的Copy构造函数和赋值运算符?

[英]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? 为什么不在这种情况下,C ++提供了一个统一的(一个函数),在这两个场景中都会调用它,而不是通过提供两个变体来复杂化?

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 . 它对已经存在的对象进行操作, 并且应该返回对self的引用 An implementation can do subtly different things here cf. 一个实现可以在这里做一些微妙的不同的事情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 . 这可以追溯到malloc (旧C方式保留内存)和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. 不同之处在于: new调用对象的构造函数,这在C ++中非常重要,否则我们将讨论除非明确指出,否则无法初始化的垃圾内存。

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). 例如,在std::vector的内部实现中,有一个size变量,用于跟踪用户使用push_back()resize (我们不是在讨论保留)时主动确认的元素数量。

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. Copy-cosntructor:必须保留内存并初始化变量,然后复制。

Now imageine that memory reserving requires checking the size and whether theArray is nullptr . 现在imageine内存预留需要检查的size和是否theArraynullptr 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. 不要把字面意思用于std::vector ,STL不能那样工作。

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

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