From: Neil Henderson on 15 Dec 2009 06:52 On Dec 15, 12:31 am, samhas <sahaselho...(a)googlemail.com> wrote: > 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. I think this behaviour is a copy elision for optimization purposes, similar to the RVO. From the C++ Standard section 12.8, paragraph 15: "when a temporary class object that has not been bound to a reference would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy" If you need the copy constructor or assignment operator to be called, you need to copy/assign an lvalue, like you did here: > test2 t = test2(20); > test2 t2(t); // or test2 t2 = t; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 Prev: Non-static singleton setup to control life time Next: Can name be reused in different block? |