public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Travis Spencer <travislspencer@gmail.com>
To: Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>
Cc: gcc-help@gcc.gnu.org
Subject: Re: copy ctor not called
Date: Sat, 02 Jul 2005 19:17:00 -0000	[thread overview]
Message-ID: <e3b7bac05070212174b4df2fb@mail.gmail.com> (raw)
In-Reply-To: <200507022024.10598.Jeroen.Wijnhout@kdemail.net>

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

This does though:

    A(const A &a) : m_i(a.m_i)
    {
        cout << "A(const A &a)" << endl;
//        m_i = a.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:

> CXXFLAGS=-fno-elide-constructors g++ -fno-elide-constructors  copyctor.cpp  -o copyctor
> ./copyctor
A(int i)
A(const A &a)

-- 

Regards,

Travis Spencer
Portland, OR USA

  reply	other threads:[~2005-07-02 19:17 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 [this message]
2005-07-03  8:15   ` Jeroen Wijnhout
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=e3b7bac05070212174b4df2fb@mail.gmail.com \
    --to=travislspencer@gmail.com \
    --cc=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).