From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23623 invoked by alias); 2 Jul 2005 19:17:53 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 23609 invoked by uid 22791); 2 Jul 2005 19:17:48 -0000 Received: from wproxy.gmail.com (HELO wproxy.gmail.com) (64.233.184.200) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Sat, 02 Jul 2005 19:17:48 +0000 Received: by wproxy.gmail.com with SMTP id i13so508684wra for ; Sat, 02 Jul 2005 12:17:47 -0700 (PDT) Received: by 10.54.34.16 with SMTP id h16mr2486568wrh; Sat, 02 Jul 2005 12:17:47 -0700 (PDT) Received: by 10.54.128.6 with HTTP; Sat, 2 Jul 2005 12:17:46 -0700 (PDT) Message-ID: Date: Sat, 02 Jul 2005 19:17:00 -0000 From: Travis Spencer Reply-To: Travis Spencer To: Jeroen Wijnhout Subject: Re: copy ctor not called Cc: gcc-help@gcc.gnu.org In-Reply-To: <200507022024.10598.Jeroen.Wijnhout@kdemail.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <200507022024.10598.Jeroen.Wijnhout@kdemail.net> X-SW-Source: 2005-07/txt/msg00017.txt.bz2 On 7/2/05, Jeroen Wijnhout wrote: > Consider a class with a copy constructor: >=20 > class A > { > public: > A(int i) : m_i(i) > { > cout << "A(int i)" << endl; > } >=20 > A(const A &a) > { > cout << "A(const A &a)" << endl; > m_i =3D a.i(); This doesn't compile: copyctor.cpp: In copy constructor `A::A(const A&)': copyctor.cpp:16: error: passing `const A' as `this' argument of `int A::i()' discards qualifiers This does though: A(const A &a) : m_i(a.m_i) { cout << "A(const A &a)" << endl; // m_i =3D a.i(); } > Now, I understand that gcc optimizes away the copy constructor and interp= rets > the code as: > A a(2); >=20 > However, is there a way to force gcc to use the copy constructor? >=20 If I were a jerk, this is where I would say to RTFM, but since I'm not I'll kindly point out the -fno-elide-constructors flag: > CXXFLAGS=3D-fno-elide-constructors g++ -fno-elide-constructors copyctor.= cpp -o copyctor > ./copyctor A(int i) A(const A &a) --=20 Regards, Travis Spencer Portland, OR USA