This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: copy ctor not called
- From: Travis Spencer <travislspencer at gmail dot com>
- To: Jeroen Wijnhout <Jeroen dot Wijnhout at kdemail dot net>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Sat, 2 Jul 2005 12:17:46 -0700
- Subject: Re: copy ctor not called
- References: <200507022024.10598.Jeroen.Wijnhout@kdemail.net>
- Reply-to: Travis Spencer <travislspencer at gmail dot com>
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