C++ 赋值运算符和复制构造函数有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11706040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:26:38  来源:igfitidea点击:

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

c++memory

提问by alan.chen

I don't understand the difference between assignment constructor and copy constructor in C++. It is like this:

我不明白 C++ 中赋值构造函数和复制构造函数之间的区别。它是这样的:

class A {
public:
    A() {
        cout << "A::A()" << endl;
    }
};

// The copy constructor
A a = b;

// The assignment constructor
A c;
c = a;

// Is it right?

I want to know how to allocate memory of the assignment constructor and copy constructor?

我想知道如何分配赋值构造函数和复制构造函数的内存?

回答by sbi

A copy constructoris used to initialize a previously uninitializedobject from some other object's data.

一个拷贝构造函数用于初始化一个先前未初始化的一些其他对象的数据对象。

A(const A& rhs) : data_(rhs.data_) {}

For example:

例如:

A aa;
A a = aa;  //copy constructor

An assignment operatoris used to replace the data of a previously initializedobject with some other object's data.

一个赋值运算符是用来替换的数据之前初始化一些其他对象的数据对象。

A& operator=(const A& rhs) {data_ = rhs.data_; return *this;}

For example:

例如:

A aa;
A a;
a = aa;  // assignment operator

You could replace copy construction by default construction plus assignment, but that would be less efficient.

您可以通过默认构造加赋值来替换复制构造,但这会降低效率。

(As a side note: My implementations above are exactly the ones the compiler grants you for free, so it would not make much sense to implement them manually. If you have one of these two, it's likely that you are manually managing some resource. In that case, per The Rule of Three, you'll very likely also need the other one plus a destructor.)

(附带说明:我上面的实现正是编译器免费授予您的实现,因此手动实现它们没有多大意义。如果您拥有这两个中的一个,则很可能您正在手动管理某些资源。在这种情况下,根据The Rule of Three,您很可能还需要另一个加上析构函数。)

回答by Arun

The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it's really not all that difficult. Summarizing:

复制构造函数和赋值运算符之间的区别给新程序员带来了很多困惑,但实际上并没有那么困难。总结:

  • If a new object has to be created before the copying can occur, the copy constructor is used.
  • If a new object does not have to be created before the copying can occur, the assignment operator is used.
  • 如果必须在复制之前创建新对象,则使用复制构造函数。
  • 如果在复制之前不必创建新对象,则使用赋值运算符。

Example for assignment operator:

赋值运算符示例:

Base obj1(5); //calls Base class constructor
Base obj2; //calls Base class default constructor
obj2 = obj1; //calls assignment operator

Example for copy constructor:

复制构造函数示例:

Base obj1(5);
Base obj2 = obj1; //calls copy constructor

回答by Luchian Grigore

The first is copy initialization, the second is just assignment. There's no such thing as assignment constructor.

第一个是复制初始化,第二个是赋值。没有赋值构造函数这样的东西。

A aa=bb;

uses the compiler-generated copy constructor.

使用编译器生成的复制构造函数。

A cc;
cc=aa;

uses the default constructor to construct cc, and then the *assignment operator** (operator =) on an already existing object.

使用默认构造函数来构造cc,然后在operator =已经存在的对象上使用*赋值运算符** ( )。

I want know how to allocate memory of the assignment constructor and copy constructor?

我想知道如何分配赋值构造函数和复制构造函数的内存?

IDK what you mean by allocate memory in this case, but if you want to see what happens, you can:

IDK在这种情况下分配内存是什么意思,但是如果您想看看会发生什么,您可以:

class A
{
public :
    A(){ cout<<"default constructor"<<endl;};
    A(const A& other){ cout<<"copy constructor"<<endl;};
    A& operator = (const A& other){cout <<"assignment operator"<<endl;}
};

I also recommend you take a look at:

我还建议你看看:

Why is copy constructor called instead of conversion constructor?

为什么调用复制构造函数而不是转换构造函数?

What is The Rule of Three?

三分法则是什么?

回答by Dev

In a simple words,

简单来说,

Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. And assignment operator is called when an already initialized object is assigned a new value from another existing object.

当从现有对象创建新对象时调用复制构造函数,作为现有对象的副本。当一个已经初始化的对象从另一个现有对象分配一个新值时,将调用赋值运算符。

Example-

例子-

t2 = t1;  // calls assignment operator, same as "t2.operator=(t1);"
Test t3 = t1;  // calls copy constructor, same as "Test t3(t1);"

回答by Mak

What @Luchian Grigore Said is implemented like this

@Luchian Grigore 所说的是这样实现的

class A
{
public :
    int a;
    A(){ cout<<"default constructor"<<endl;};
    A(const A& other){ cout<<"copy constructor"<<endl;};
    A& operator = (const A& other){cout <<"assignment operator"<<endl;}
};

void main()
{
    A sampleObj; //Calls default constructor
    sampleObj.a = 10;

    A copyConsObj  = sampleObj; //Initializing calls copy constructor

    A assignOpObj; //Calls default constrcutor
    assignOpObj = sampleObj; //Object Created before so it calls assignment operator
}

OUTPUT

输出



default constructor

默认构造函数



copy constructor

复制构造函数



default constructor

默认构造函数



assignment operator

赋值运算符



回答by Nitesh Kumar

the difference between a copy constructor and an assignment constructor is:

复制构造函数和赋值构造函数之间的区别是:

  1. In case of a copy constructor it creates a new object.(<classname> <o1>=<o2>)
  2. In case of an assignment constructor it will not create any object means it apply on already created objects(<o1>=<o2>).
  1. 如果是复制构造函数,它会创建一个新对象。( <classname> <o1>=<o2>)
  2. 在赋值构造函数的情况下,它不会创建任何对象,这意味着它适用于已经创建的对象(<o1>=<o2>)。

And the basic functionalities in both are same, they will copy the data from o2 to o1 member-by-member.

并且两者的基本功能是相同的,它们会逐个成员地将数据从 o2 复制到 o1。

回答by MD BELAL RASHID

I want to add one more point on this topic. "The operator function of assignment operator should be written only as a member function of the class." We can't make it as friend function unlike other binary or unary operator.

我想就这个话题再补充一点。“赋值运算符的运算符函数应该只写成类的成员函数。” 与其他二元或一元运算符不同,我们不能将其作为友元函数。

回答by Frank Shen

Something to add about copy constructor:

关于复制构造函数的一些补充:

  • When passing an object by value, it will use copy constructor

  • When an object is returned from a function by value, it will use copy constructor

  • When initializing an object using the values of another object(as the example you give).

  • 按值传递对象时,它将使用复制构造函数

  • 当一个对象按值从函数返回时,它将使用复制构造函数

  • 使用另一个对象的值初始化对象时(如您提供的示例)。