468,247 Members | 1,293 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

copy constructor Q

If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David

Mar 3 '06 #1
7 1640

David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David

aa::aa() and an implicit aa::= are invoked. If your code is 'aa
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.

Mar 3 '06 #2
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David
aa::aa() and an implicit aa::= are invoked. If your code is 'aa


Erm, no. This is an initialization; there is no assignment involved.
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.
Similarly.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 3 '06 #3
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David
aa::aa() and an implicit aa::= are invoked.


No assignment operator is involved in this. Only default-construction and
copy-construction. And most likely, with the allowed optimization, no
copy-construction is happening.
If your code is 'aa
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.


V
--
Please remove capital As from my address when replying by mail
Mar 3 '06 #4

Artie Gold wrote:
Fei Liu wrote:
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz? The
compiler I use simply invokes the aa(), but I am wondering based on the
comeau compiler output if this is a correct interpretation of the
standard, or if both constructors are supposed to be invoked?

Thanks

David


aa::aa() and an implicit aa::= are invoked. If your code is 'aa


Erm, no. This is an initialization; there is no assignment involved.
zz(aa())' then aa::aa() and aa::aa(const aa&t) are invoked.


Similarly.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]


What if the code is
aa zz;
zz=aa();

So in 'aa zz=aa();', '=' is equivalent of calling a copy constructor?

I guess it's time to revisit the book.

Mar 3 '06 #5
Fei Liu wrote:
Artie Gold wrote:
Fei Liu wrote:
David Lindauer wrote:
[..]
aa zz = aa();
[..]
aa::aa() and an implicit aa::= are invoked. If your code is 'aa
Erm, no. This is an initialization; there is no assignment involved.

[..]
What if the code is
aa zz;
zz=aa();


But the code _isn't_. Yes, if you change the code, you default-initialise
the 'zz' and then assign it on the next statement.
So in 'aa zz=aa();', '=' is equivalent of calling a copy constructor?
The syntax is called "copy-initialisation". Look it up.
I guess it's time to revisit the book.


Good call!
Mar 3 '06 #6
David Lindauer wrote:
If I do this:

struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};

int tt()
{
aa zz = aa();
}

which constructor(s) are supposed to be invoked when creating zz?
Two possibilities: Either the default-constructor is called to create a
temporary, then the copy constructor is called to create zz from that. The
other possibility is that the temporary gets optimized away (which is
allowed by the standard) and so zz is created using the default
constructor.
However, even if the copy constructor is not used, it still must be
available, because optimizations can't change the correctness of code.
The compiler I use simply invokes the aa(), but I am wondering based on
the comeau compiler output
What output?
if this is a correct interpretation of the standard, or if both
constructors are supposed to be invoked?


Mar 4 '06 #7


Rolf Magnus wrote:
The compiler I use simply invokes the aa(), but I am wondering based on
the comeau compiler output


What output?


when I ran what I typed through the comeau online compiler, with the addition
of declaring both constructors explicit, I was given an error that there was
no suitable copy constructor available. That is why I was asking the
question... and your answer helped me see why it would. I didn't realize the
standard gave explicit instructions as to certain allowable optimizations and
was glad I asked :).

David
Mar 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Edward Diener | last post: by
15 posts views Thread by A | last post: by
8 posts views Thread by Jesper | last post: by
10 posts views Thread by utab | last post: by
8 posts views Thread by shuisheng | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.