/* Hi,
in the following program the copy constructor of class C is called with the constructor argument having the same address as the to be constructed object. Best regards, Oliver Schmidt $ for i in g++-3.3 g++-3.4 g++-4.0; do echo "================"; $i --version; $i -Wall t3.cpp; a.out ; echo; done ================ g++-3.3 (GCC) 3.3.5 (Debian 1:3.3.5-13) Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1) getValue() -> 100, getValue() -> 101 2) getValue() -> 200, getValue() -> 201 3) getValue() -> 300, XXX getValue() -> 201 4) getValue() -> 400, getValue() -> 400 ================ g++-3.4 (GCC) 3.4.4 20050314 (prerelease) (Debian 3.4.3-13) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1) getValue() -> 100, getValue() -> 101 2) getValue() -> 200, getValue() -> 201 3) getValue() -> 300, XXX getValue() -> 201 4) getValue() -> 400, XXX getValue() -> 201 ================ g++-4.0 (GCC) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1) getValue() -> 100, getValue() -> 101 2) getValue() -> 200, getValue() -> 201 3) getValue() -> 300, XXX getValue() -> 201 4) getValue() -> 400, XXX getValue() -> 201 */ /////////////////////////////////////////////////////////////////////////////// #include <stdio.h> class C { public: C() : v(counter++) {} C(const C& rhs) { v = rhs.v; if (this == &rhs) { printf("XXX "); } } int getValue() { return v; } void setValue(int v) { this->v = v; } static void setCounter(int i) { counter = i; } private: int v; static int counter; }; int C::counter = 100; class D { public: int getValue() { return c.getValue(); } private: C c; }; void f1() { D d1; printf("1) getValue() -> %d, ", d1.getValue()); { D d1; printf("getValue() -> %d\n", d1.getValue()); } } void f2() { D d2; printf("2) getValue() -> %d, ", d2.getValue()); { D d2; d2 = d2; printf("getValue() -> %d\n", d2.getValue()); } } void f3() { D d3; printf("3) getValue() -> %d, ", d3.getValue()); { D d3 = d3; printf("getValue() -> %d\n", d3.getValue()); } } void f4() { D d4; printf("4) getValue() -> %d, ", d4.getValue()); { D d4(d4); printf("getValue() -> %d\n", d4.getValue()); } } int main(int argc, char **argv) { C::setCounter(100); f1(); C::setCounter(200); f2(); C::setCounter(300); f3(); C::setCounter(400); f4(); return 0; } /////////////////////////////////////////////////////////////////////////////// -- Summary: Copy constructor is called with "this == &rhs" Product: gcc Version: 4.0.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: oschmidt at gmx dot net http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29117