分配运算符和复制构造函数之间的区别是什么?

发布于 2025-02-13 08:21:26 字数 316 浏览 2 评论 0 原文

我不了解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 don't understand the difference between assignment constructor and copy constructor in C++. It is like this:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

纵情客 2025-02-20 08:21:26

A 复制构造函数 用于从其他对象的数据中初始化 以前非初始化的 对象。

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

例如:

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

分配运算符 用于用其他对象的数据替换 先前初始化 对象的数据。

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

例如:

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

您可以通过默认的施工加分配替换复制构造,但这效率较低。

(作为旁注:我上面的实现完全是编译器免费授予您的,因此手动实施它们是没有多大意义的。如果您拥有这两个方面之一,则您可能正在手动管理某些资源。在这种情况下,per 三个 ,您很可能还需要另一个加上灾难。)

A copy constructor is used to initialize a previously uninitialized object from some other object's data.

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

For example:

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

An assignment operator is used to replace the data of a previously initialized object 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.)

末蓝 2025-02-20 08:21:26

复制构造函数和分配运算符之间的区别会给新程序员带来很多混乱,但这并不是那么困难。总结:

  • 如果在复制之前必须创建一个新对象,则使用复制构造函数。
  • 如果不必在复制之前创建新对象,则使用分配运算符。

分配操作员的示例:

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

复制构造函数的示例:

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

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
—━☆沉默づ 2025-02-20 08:21:26

第一个是复制初始化,第二个就是分配。没有分配构造器之类的东西。

A aa=bb;

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

A cc;
cc=aa;

使用默认构造函数来构造 cc ,然后在已经存在的对象上*分配操作员**( operator = )。

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

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

我也建议您查看:

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

三个规则是什么?

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.

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:

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?

雪落纷纷 2025-02-20 08:21:26

简单地说,

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

例子-

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

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);"
百合的盛世恋 2025-02-20 08:21:26

复制构造函数和分配构造函数之间的区别是:

  1. 如果复制构造函数,则创建一个新对象。(&lt; className&gt;&lt; o1&gt; =&lt; =&lt; o2&gt;
  2. )分配构造函数它不会创建任何对象,意味着它应用于已经创建的对象(&lt; o1&gt; =&lt; o2&gt; )。

并且两者的基本功能都是相同的,它们将将数据从O2复制到成员。

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>).

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

心房敞 2025-02-20 08:21:26

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

输出


默认构造函数复制


构造函数


默认构造函数


分配符


What @Luchian Grigore Said is implemented like this

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


装迷糊 2025-02-20 08:21:26

我想在这个主题上再增加一点。
“分配操作员的操作员函数应仅写为课程的成员函数。”与其他二进制或一元操作员不同,我们无法成为朋友功能。

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.

情徒 2025-02-20 08:21:26

要添加的副本构造函数:

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

  • 按值传递对象时,当对象从函数返回函数时

    值,它将使用复制构造函数

  • 在函数返回一个对象时(作为您给出的示例)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文