Reference variable assignment and copy constructor call
-
I am testing the constructor call with the code below:
#include class A
{
public:
A(int n = 0)
: m_n(n)
{} A(const A& a) : m\_n(a.m\_n) { }
private:
int m_n;
};int main()
{
A a(2), b(1);const A c(a); const A &d = c; const A e = b; //Note 1 b = d; //Note 2: I am confused here return 0;
}
Everything is OK with me. But there is one point that I cannot understand is that: At the line "b = d" (Note 2), why the copy constructor is not called while at the line "const A e = b" (Note 2) the constructor is called? I expect that at line Note 2, the copy constructor will be called to copy the value of "c" (referenced by "d") to "b", but it doesn't happen. Please explain it, thank you in advance!
-
I am testing the constructor call with the code below:
#include class A
{
public:
A(int n = 0)
: m_n(n)
{} A(const A& a) : m\_n(a.m\_n) { }
private:
int m_n;
};int main()
{
A a(2), b(1);const A c(a); const A &d = c; const A e = b; //Note 1 b = d; //Note 2: I am confused here return 0;
}
Everything is OK with me. But there is one point that I cannot understand is that: At the line "b = d" (Note 2), why the copy constructor is not called while at the line "const A e = b" (Note 2) the constructor is called? I expect that at line Note 2, the copy constructor will be called to copy the value of "c" (referenced by "d") to "b", but it doesn't happen. Please explain it, thank you in advance!
Hi, You are not invoking the assignment operator with your
const A e = b;
. You are constructing a new object ofconst A e
and performing initialization by copy ofb
. This is covered in section 8.5 paragraph 15 of the latest standard. www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^] Best Wishes, -David Delaune -
Hi, You are not invoking the assignment operator with your
const A e = b;
. You are constructing a new object ofconst A e
and performing initialization by copy ofb
. This is covered in section 8.5 paragraph 15 of the latest standard. www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^] Best Wishes, -David DelauneThanks Randor for your prompt reply. As your answer, I can conclude that the copy constructor will be call for initialization only, not for "=" (assignment operator). Since long time ago, I have believed that the copy constructor will be call for "=" operator if there is no "=" operator defined for that class. But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy? Thanks.
-
Thanks Randor for your prompt reply. As your answer, I can conclude that the copy constructor will be call for initialization only, not for "=" (assignment operator). Since long time ago, I have believed that the copy constructor will be call for "=" operator if there is no "=" operator defined for that class. But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy? Thanks.
Hi,
trinh.nguyen wrote:
But I still wonder this: If there is no "=" operator defined for that class, what will be implemented for "=" operator? Binary copy?
If you do not write an assignment operator for your class the compiler will generate a default assignment operator that performs a memberwise copy[^] of base classes and members. Have a look at section 12.8 paragraph 15: www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf[^] Best Wishes, -David Delaune