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.
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
consttonon-constqualification forstring literals(4.2) is deprecated.So to avoid the warning You should use: