public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>
To: gcc-help@gcc.gnu.org
Subject: Re: copy ctor not called
Date: Sun, 03 Jul 2005 08:15:00 -0000	[thread overview]
Message-ID: <200507031017.54129.Jeroen.Wijnhout@kdemail.net> (raw)
In-Reply-To: <e3b7bac05070212174b4df2fb@mail.gmail.com>

On Saturday 02 July 2005 09:17 pm, Travis Spencer wrote:
> On 7/2/05, Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net> 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

  reply	other threads:[~2005-07-03  8:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-02 18:21 Jeroen Wijnhout
2005-07-02 19:17 ` Travis Spencer
2005-07-03  8:15   ` Jeroen Wijnhout [this message]
2005-07-03 12:27 ` Dirk Jagdmann
2005-07-03 13:44   ` Jeroen Wijnhout
2005-07-05 14:10     ` Eljay Love-Jensen
2005-07-07 11:45       ` Jeroen Wijnhout
2005-07-07 12:02       ` Eljay Love-Jensen
2005-07-08 18:26         ` Jeroen Wijnhout

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200507031017.54129.Jeroen.Wijnhout@kdemail.net \
    --to=jeroen.wijnhout@kdemail.net \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).