* Copy constructor not called.
@ 2009-08-12 10:03 Mustafa4LP
2009-08-12 11:28 ` Sven Eschenberg
2009-08-12 20:04 ` John (Eljay) Love-Jensen
0 siblings, 2 replies; 4+ messages in thread
From: Mustafa4LP @ 2009-08-12 10:03 UTC (permalink / raw)
To: gcc-help
Hi all,
Please consider the following program:
#include <iostream>
using namespace std;
class A
{
public:
A ()
{
cout << "constructor" << endl;
}
A (A &a)
{
cout << "copy constructor" << endl;
}
A operator = (const A &a)
{
cout << "= operator" << endl;
}
};
int main()
{
A a;
A b;
b = ( b = a );
}
Following error is issued while running the above test case on gcc 3.4.6
test007.cpp: In function `int main()':
test007.cpp:23: error: no matching function for call to `A::A(A)'
test007.cpp:11: note: candidates are: A::A(A&)
However, if I change the argument of copy constructor to "const A &", the
program compiles finely.
I could not understand why const is required in copy constructor for running
the test case.
Also note that in the output of the modified program (which compiled
finely), no copy constructor is called.
I am running the test case with --no-elide-constructors option to disable
the RVO (so that I can get all copy constructor calls).
Thanks in advance for any help.
Regards,
Mustafa
--
View this message in context: http://www.nabble.com/Copy-constructor-not-called.-tp24930356p24930356.html
Sent from the gcc - Help mailing list archive at Nabble.com.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Copy constructor not called.
2009-08-12 10:03 Copy constructor not called Mustafa4LP
@ 2009-08-12 11:28 ` Sven Eschenberg
2009-08-12 20:04 ` John (Eljay) Love-Jensen
1 sibling, 0 replies; 4+ messages in thread
From: Sven Eschenberg @ 2009-08-12 11:28 UTC (permalink / raw)
To: Mustafa4LP, GCC-help
As far as I can tell:
Your assignment Operator does not return a reference, that's why the
compiler complains about not finding a matching copy contructor. Afaik
semantically ( b = a ) would yield a copy constructor to create a
temporary for the RV, which is A and not A & in your case. I don't know
though, why a const in the copy constructor fixes the compile error as well.
And I want to quote from the man page:
-fno-elide-constructors
The C++ standard allows an implementation to omit creating a
tempo-
rary which is only used to initialize another object of the same
type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.
As I read this this only relates to temporarys used for initialization.
I don't see any initialization, only a RV temporary passed to the
assignment operator. Thus the RVO will still be done and not be disabled
by that option, as I read it.
Regards
-Sven
Mustafa4LP schrieb:
> Hi all,
>
> Please consider the following program:
>
> #include <iostream>
> using namespace std;
> class A
> {
> public:
> A ()
> {
> cout << "constructor" << endl;
> }
> A (A &a)
> {
> cout << "copy constructor" << endl;
> }
> A operator = (const A &a)
> {
> cout << "= operator" << endl;
> }
> };
> int main()
> {
> A a;
> A b;
> b = ( b = a );
> }
>
>
> Following error is issued while running the above test case on gcc 3.4.6
>
> test007.cpp: In function `int main()':
> test007.cpp:23: error: no matching function for call to `A::A(A)'
> test007.cpp:11: note: candidates are: A::A(A&)
>
> However, if I change the argument of copy constructor to "const A &", the
> program compiles finely.
> I could not understand why const is required in copy constructor for running
> the test case.
>
> Also note that in the output of the modified program (which compiled
> finely), no copy constructor is called.
> I am running the test case with --no-elide-constructors option to disable
> the RVO (so that I can get all copy constructor calls).
>
> Thanks in advance for any help.
>
> Regards,
> Mustafa
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Copy constructor not called.
2009-08-12 10:03 Copy constructor not called Mustafa4LP
2009-08-12 11:28 ` Sven Eschenberg
@ 2009-08-12 20:04 ` John (Eljay) Love-Jensen
2009-08-13 9:28 ` Mustafa4LP
1 sibling, 1 reply; 4+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-08-12 20:04 UTC (permalink / raw)
To: Mustafa4LP, GCC-help
Hi Mustafa,
> Please consider the following program:
>
> #include <iostream>
> using namespace std;
> class A
> {
> public:
> A ()
> {
> cout << "constructor" << endl;
> }
> A (A &a)
> {
> cout << "copy constructor" << endl;
> }
> A operator = (const A &a)
> {
> cout << "= operator" << endl;
> }
> };
> int main()
> {
> A a;
> A b;
> b = ( b = a );
> }
Your "copy constructor" is not an appropriate copy constructor.
Change it to:
A(A const& a)
{
cout << "copy constructor" << endl;
}
Also, your assignment operator is not an appropriate assignment operator.
Change it to:
A const& operator = (A const& a)
{
cout << "= operator" << endl;
return *this;
}
HTH,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Copy constructor not called.
2009-08-12 20:04 ` John (Eljay) Love-Jensen
@ 2009-08-13 9:28 ` Mustafa4LP
0 siblings, 0 replies; 4+ messages in thread
From: Mustafa4LP @ 2009-08-13 9:28 UTC (permalink / raw)
To: gcc-help
Hi Eljay,
I know that design wise the copy constructor and assignment operator are not
correct.
Actually, I am not writing the code for application but trying some test
case to understand C++ better.
The original program that I posted was correct in terms of C++ standards, it
came out to be a bug of GCC version 3.4.6.
When I tried the same on GCC version 4.3 it executed as expected.
Thanks for the help.
Regards,
Mustafa
John (Eljay) Love-Jensen wrote:
>
> Hi Mustafa,
>
>> Please consider the following program:
>>
>> #include <iostream>
>> using namespace std;
>> class A
>> {
>> public:
>> A ()
>> {
>> cout << "constructor" << endl;
>> }
>> A (A &a)
>> {
>> cout << "copy constructor" << endl;
>> }
>> A operator = (const A &a)
>> {
>> cout << "= operator" << endl;
>> }
>> };
>> int main()
>> {
>> A a;
>> A b;
>> b = ( b = a );
>> }
>
> Your "copy constructor" is not an appropriate copy constructor.
>
> Change it to:
>
> A(A const& a)
> {
> cout << "copy constructor" << endl;
> }
>
> Also, your assignment operator is not an appropriate assignment operator.
>
> Change it to:
>
> A const& operator = (A const& a)
> {
> cout << "= operator" << endl;
> return *this;
> }
>
> HTH,
> --Eljay
>
>
>
--
View this message in context: http://www.nabble.com/Copy-constructor-not-called.-tp24930356p24949027.html
Sent from the gcc - Help mailing list archive at Nabble.com.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-08-13 5:22 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-12 10:03 Copy constructor not called Mustafa4LP
2009-08-12 11:28 ` Sven Eschenberg
2009-08-12 20:04 ` John (Eljay) Love-Jensen
2009-08-13 9:28 ` Mustafa4LP
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).