分配运算符和复制构造函数之间的区别是什么?
我不了解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?
我想知道如何分配分配构造函数和复制构造函数的内存?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
A 复制构造函数 用于从其他对象的数据中初始化 以前非初始化的 对象。
例如:
分配运算符 用于用其他对象的数据替换 先前初始化 对象的数据。
例如:
您可以通过默认的施工加分配替换复制构造,但这效率较低。
(作为旁注:我上面的实现完全是编译器免费授予您的,因此手动实施它们是没有多大意义的。如果您拥有这两个方面之一,则您可能正在手动管理某些资源。在这种情况下,per 三个 ,您很可能还需要另一个加上灾难。)
A copy constructor is used to initialize a previously uninitialized object from some other object's data.
For example:
An assignment operator is used to replace the data of a previously initialized object with some other object's data.
For example:
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 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:
Example for assignment operator:
Example for copy constructor:
第一个是复制初始化,第二个就是分配。没有分配构造器之类的东西。
使用编译器生成的复制构造函数。
使用默认构造函数来构造
cc
,然后在已经存在的对象上*分配操作员**(operator =
)。idk在这种情况下,分配内存的意思是什么,但是如果您想看看会发生什么,可以:
我也建议您查看:
为什么称为复制构造函数而不是转换构造函数?
三个规则是什么?
The first is copy initialization, the second is just assignment. There's no such thing as assignment constructor.
uses the compiler-generated copy constructor.
uses the default constructor to construct
cc
, and then the *assignment operator** (operator =
) on an already existing object.IDK what you mean by allocate memory in this case, but if you want to see what happens, you can:
I also recommend you take a look at:
Why is copy constructor called instead of conversion constructor?
What is The Rule of Three?
简单地说,
当从现有对象创建新对象作为现有对象的副本时,调用复制构造函数。
当从另一个现有对象分配一个已经初始化的对象时,分配运算符被调用。
例子-
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-
复制构造函数和分配构造函数之间的区别是:
&lt; className&gt;&lt; o1&gt; =&lt; =&lt; o2&gt;
&lt; o1&gt; =&lt; o2&gt;
)。并且两者的基本功能都是相同的,它们将将数据从O2复制到成员。
the difference between a copy constructor and an assignment constructor is:
<classname> <o1>=<o2>
)<o1>=<o2>
).And the basic functionalities in both are same, they will copy the data from o2 to o1 member-by-member.
@luchian Grigore所说的内容像此
输出
默认构造函数复制
构造函数
默认构造函数
分配符
What @Luchian Grigore Said is implemented like this
OUTPUT
default constructor
copy constructor
default constructor
assignment operator
我想在这个主题上再增加一点。
“分配操作员的操作员函数应仅写为课程的成员函数。”与其他二进制或一元操作员不同,我们无法成为朋友功能。
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.
要添加的副本构造函数:
按值传递对象时,它将使用复制构造函数
值,它将使用复制构造函数
在函数返回一个对象时(作为您给出的示例)。
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).