复制构造函数和赋值运算符 [英] The copy constructor and assignment operator

查看:164
本文介绍了复制构造函数和赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我覆盖operator =将复制构造函数自动使用新的运算符?类似地,如果我定义一个复制构造函数,将操作符=从复制构造函数自动继承行为。

If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically 'inherit' the behavior from the copy constructor?

推荐答案

他们是不同的运营商。

No, they are different operators.

复制构造函数用于创建一个新对象。它将现有对象复制到新构造的对象。复制构造函数用于从旧的
实例初始化新实例。当将变量按值传递给函数
或作为函数中的返回值时,不一定会调用它。

The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by value into functions or as return values out of functions.

赋值运算符用于处理已经存在的对象。赋值运算符用于更改现有实例以使
与rvalue具有相同的值,这意味着如果实例具有内部动态内存,则必须销毁并重新初始化实例

The assignment operator is to deal with an already existing object. The assignment operator is used to change an existing instance to have the same values as the rvalue, which means that the instance has to be destroyed and re-initialized if it has internal dynamic memory.

有用的链接:

  • Copy Constructors, Assignment Operators, and More
  • Copy constructor and = operator overload in C++: is a common function possible?

这篇关于复制构造函数和赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆