默认复制构造函数和默认赋值运算符

发布于 2024-11-28 15:35:29 字数 710 浏览 1 评论 0原文

我在此处使用过的代码中使用的这些之间有什么区别。(第 44 行和第 45 行都工作正常)

摘录:

Date temp = *this;  //ASSIGNMENT OPERATOR CALL(PROVIDED BY COMPILER)
//Date temp(*this); //COPY CONSTRUCTOR CALL(PROVIDED BY COMPILER)

我的意见:是否在赋值过程中像 object1 = object2; object2 的内容被删除并放置在 object1 中,而如果通过复制构造函数发生同样的事情,object2 的内容仍然保留(我的意思就像这个词暗示的“复制”)。

注意:顺便说一句,我的代码在 Microsoft Visual C++ 2008 中编译得很好,但

  prog.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Date&)’:
  prog.cpp:103: warning: deprecated conversion from string constant to 

在 ideone.com 中给出了警告。有任何原因吗?

what is the difference between either of these used in a code as i have used here.(line 44 ad line 45 both work fine)

Excerpt:

Date temp = *this;  //ASSIGNMENT OPERATOR CALL(PROVIDED BY COMPILER)
//Date temp(*this); //COPY CONSTRUCTOR CALL(PROVIDED BY COMPILER)

My Opinion : Is it that during assignment like object1 = object2; contents of object2 are deleted and placed in object1 while while if the same thing happens via a copy constructor contents of object2 still remain( i mean just as the word suggests "copy").

NOTE: By the way my code compiled fine in Microsoft visual C++ 2008 but it gave a warning

  prog.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Date&)’:
  prog.cpp:103: warning: deprecated conversion from string constant to 

in ideone.com.Any reasons for that.

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

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

发布评论

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

评论(3

尽揽少女心 2024-12-05 15:35:29
Date temp = *this; 
Date temp(*this); 

两者都调用复制构造函数,
首先称为复制初始化 &第二个称为直接初始化

记住这一点的简单规则是:
如果在同一语句中创建并初始化一个对象,则它会调用 Copy 构造函数。

如果一个对象刚刚被赋值并且没有在同一语句中创建,那么它会调用赋值(复制赋值)运算符。


编译器抱怨是因为:

普通字符串文字的类型为“n const char 的数组”。并且不推荐使用字符串文字 (4.2) 从 const 到非常量限定的隐式转换。

参考资料:
C++ 标准部分 [2.13.4/2]:
普通字符串文字的类型为n const char的数组,其中n是字符串的大小,如下定义;它具有静态存储持续时间 (3.7) 并使用给定字符进行初始化。

附件 D 部分 [D.4/1]
constnon-const资格的字符串文字(4.2)的隐式转换已被弃用。

因此要避免您应该使用的警告:

static const char *monthName[13]
      ^^^^^^^ 
Date temp = *this; 
Date temp(*this); 

Both call copy constructor,
First is called as Copy Initialization & second is called as Direct Initialization.

Simple Rule to remember this is:
If an object is getting created as well as initialized in the same statement then it calls Copy constructor.

If an object is just assigned and not being created in the same statement then it calls Assignment(Copy Assignment) operator.


The compiler complains because:

An ordinary string literal has type “array of n const char”. And implicit conversion from const to non-const qualification for string literals (4.2) is deprecated.

References:
C++ Standard Section [2.13.4/2]:
An ordinary string literal has type array of n const char, where n is the size of the string as defined below; it has static storage duration (3.7) and is initialized with the given characters.

Annexure D section [D.4/1]
The implicit conversion from const to non-const qualification for string literals (4.2) is deprecated.

So to avoid the warning You should use:

static const char *monthName[13]
      ^^^^^^^ 
謸气贵蔟 2024-12-05 15:35:29
static char *monthName[13]

可以/应该

static const char *monthName[13]

避免警告。

static char *monthName[13]

can/should be

static const char *monthName[13]

to avoid the warning.

南冥有猫 2024-12-05 15:35:29

默认复制构造函数和默认赋值运算符之间的区别在于,当调用赋值运算符时,接收对象的成员已经用值进行了初始化,您只需将它们替换为第二个对象中的值的副本即可。使用复制构造函数,成员将被初始化为第二个对象中的成员的副本。第二个对象应该完全不受任一操作的影响。

但是,您没有在代码中使用赋值运算符,而是使用复制构造函数。如果您这样做了:

Date temp;
temp = *this;

那么您将使用赋值运算符。

The difference between the default copy constructor and the default assignment operator is that when the assignment operator is called, the members of the receiving object are already initialized with values, and you are simply replacing them with copies of the values in the second object. With the copy constructor, the members are initialized as copies of the members in the second object. The second object should be completely unaffected by either operation.

However, you are not using the assignment operator in your code, you are using the copy constructor. If you had done this:

Date temp;
temp = *this;

Then you would be using the assignment operator.

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