From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 835 invoked by alias); 3 Jul 2005 08:15:07 -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 814 invoked by uid 22791); 3 Jul 2005 08:15:00 -0000 Received: from smtp14.wxs.nl (HELO smtp14.wxs.nl) (195.121.6.28) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Sun, 03 Jul 2005 08:15:00 +0000 Received: from brain.thuis (ip503d51d6.speed.planet.nl [80.61.81.214]) by smtp14.wxs.nl (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTP id <0IJ100DYWLKUJV@smtp14.wxs.nl> for gcc-help@gcc.gnu.org; Sun, 03 Jul 2005 10:14:58 +0200 (CEST) Date: Sun, 03 Jul 2005 08:15:00 -0000 From: Jeroen Wijnhout Subject: Re: copy ctor not called In-reply-to: To: gcc-help@gcc.gnu.org Message-id: <200507031017.54129.Jeroen.Wijnhout@kdemail.net> MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Content-disposition: inline User-Agent: KMail/1.8.50 References: <200507022024.10598.Jeroen.Wijnhout@kdemail.net> X-SW-Source: 2005-07/txt/msg00018.txt.bz2 On Saturday 02 July 2005 09:17 pm, Travis Spencer wrote: > On 7/2/05, Jeroen Wijnhout wrote: > > Consider a class with a copy constructor: > > > > class A > > { > > public: > > A(int i) : m_i(i) > > { > > cout << "A(int i)" << endl; > > } > > > > A(const A &a) > > { > > cout << "A(const A &a)" << endl; > > m_i = 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 Sorry about that, should have been: int i() const { return m_i; } > > Now, I understand that gcc optimizes away the copy constructor and > > interprets the code as: > > A a(2); > > > > However, is there a way to force gcc to use the copy constructor? > > 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: Actually I did, missed that, but thanks for pointing it out. ;-) > > CXXFLAGS=-fno-elide-constructors g++ -fno-elide-constructors > > copyctor.cpp -o copyctor ./copyctor Indeed this works as it should. best, Jeroen -- Kile -- KDE Integrated LaTeX Environment http://kile.sourceforge.net