From: samhas on 14 Dec 2009 07:31 Hello, I have the following class definition: class test2{ public: int y; test2(int ui) : y(ui){} test2(const test2& t) : y(5){} test2& operator= (const test2& t){ y = 6; return *this; } }; In an expression like test2 t2(test2(20)); or test2(t2) = test2(20); neither the copy-constructor nor the assignment operator gets executed. t2 ends up with y=20. But in the following case the copy constructor gets called: test2 t = test2(20); test2 t2(t); // or test2 t2 = t; Can someone explain to me what's going on there? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 14 Dec 2009 23:11 samhas wrote: > Hello, > > I have the following class definition: > > class test2{ > public: > int y; If you are going to have public data declare the class with the struct keyword. That can serve as a wakeup call. > test2(int ui) : y(ui){} > test2(const test2& t) : y(5){} > test2& operator= (const test2& t){ y = 6; return *this; } > > }; > > In an expression like > > test2 t2(test2(20)); or > test2(t2) = test2(20); > > neither the copy-constructor nor the assignment operator gets > executed. > t2 ends up with y=20. > > But in the following case the copy constructor gets called: > > test2 t = test2(20); > test2 t2(t); // or test2 t2 = t; > > Can someone explain to me what's going on there? > The '=' in a declaration is not an assignment so operator=() would not be called. However the compiler can in many cases elide the call of a copy ctor even if doing so suppresses side effects. All that is happening in is that the compiler is exercising that freedom. It does not have to justify when it chooses to do so as long as it does whenever the Standard actually requires a call of the copy ctor. It is required in the case: test2 t2(t); and in the case test2 t2 = t; Because in those cases it is the only ctor that can construct t2. But in the other cases another ctor has to be called first so the compiler can elide the copy ctor. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 14 Dec 2009 23:12 On 15 d�c, 00:31, samhas <sahaselho...(a)googlemail.com> wrote: > But in the following case the copy constructor gets called: > > test2 t = test2(20); > test2 t2(t); // or test2 t2 = t; > > Can someone explain to me what's going on there? You're copying t to t2. No way to elide any copies here... -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bart van Ingen Schenau on 14 Dec 2009 23:10 On Dec 15, 1:31 am, samhas <sahaselho...(a)googlemail.com> wrote: > Hello, > > I have the following class definition: > > class test2{ > public: > int y; > test2(int ui) : y(ui){} > test2(const test2& t) : y(5){} > test2& operator= (const test2& t){ y = 6; return *this; } > > }; > > In an expression like > > test2 t2(test2(20)); or > test2(t2) = test2(20); > > neither the copy-constructor nor the assignment operator gets > executed. > t2 ends up with y=20. > > But in the following case the copy constructor gets called: > > test2 t = test2(20); > test2 t2(t); // or test2 t2 = t; > > Can someone explain to me what's going on there? First, even though some declarations use the =-sign, it is not an assignment, so the assignment operator will not be used. In the declaration 'test2 t2(test2(20));', you are constructing a new object from a temporary object. For this situation, the compiler has a special dispensation that it can eliminate the temporary object and directly initialise the named object from the source of the temporary object even if that optimisation causes an observable effect. So, your compiler is interpreting it as being identical to 'test2 t2 (20);' and is fully withing its rights to do so. Bart v Ingen Schenau -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: samhas on 15 Dec 2009 06:58 thx for your replies - this explains the issue :) So I think it's better to be careful when initializing an object with a copy of some temporary... K. H. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: Non-static singleton setup to control life time Next: Can name be reused in different block? |