简体   繁体   English

默认的Copy构造函数和默认的赋值运算符

[英]default Copy constructer and default assignment operator

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) 我在这里使用的代码中使用的这两者之间有什么区别(第44行和第45行都可以正常工作)

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; 我的意见 :是在分配期间像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"). 对象2的内容将被删除并放置在对象1中,而如果通过复制构造函数发生相同的情况,则对象2的内容仍将保留(我的意思是,正如单词暗示“复制”一样)。

NOTE: By the way my code compiled fine in Microsoft visual C++ 2008 but it gave a warning 注意:顺便说一下,我的代码在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 

in ideone.com.Any reasons for that. 在ideone.com中的任何原因。

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. 如果要在同一条语句中创建和初始化对象,则它将调用Copy构造函数。

If an object is just assigned and not being created in the same statement then it calls Assignment(Copy Assignment) operator. 如果只是分配了一个对象而未在同一语句中创建该对象,则它将调用Assignment(Copy Assignment)运算符。


The compiler complains because: 编译器抱怨是因为:

An ordinary string literal has type “array of n const char”. 普通字符串文字的类型为“ n const char数组”。 And implicit conversion from const to non-const qualification for string literals (4.2) is deprecated. 并且不建议将字符串文字(4.2)从const隐式转换为非const限定。

References: 参考文献:
C++ Standard Section [2.13.4/2]: C ++标准节[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; 普通字符串文字的类型array of n const char ,其中n是如下定义的字符串的大小; it has static storage duration (3.7) and is initialized with the given characters. 它具有静态存储持续时间(3.7),并使用给定的字符进行了初始化。

Annexure D section [D.4/1] 附件D节[D.4 / 1]
The implicit conversion from const to non-const qualification for string literals (4.2) is deprecated. non-conststring literals (4.2)从const隐式转换non-const限定。

So to avoid the warning You should use: 因此,为了避免警告,您应该使用:

static const char *monthName[13]
      ^^^^^^^ 
static char *monthName[13]

can/should be 可以/应该是

static const char *monthName[13]

to avoid the warning. 避免警告。

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. 使用copy构造函数,将成员初始化为第二个对象中成员的副本。 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. 然后,您将使用赋值运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM