initialization syntax

T

thomas

what's the difference between these two statements?

1.
class A{};
A temp(b); A a = temp;

2.
A a = A(b);

the first works as expected
for the 2nd, compiler complains that there's no function A:A(A).

I don't get it. IMHO, they should be the same.
 
D

dizzy

thomas said:
what's the difference between these two statements?

1.
class A{};
A temp(b);

This won't compile as you try to initialize A with a ctor that would
take a single argument and you have no such ctor defined.
A a = temp;

Initialization as if you would have written instead "A a(temp);"
(notice that this is true ONLY because temp is the same type as A, if
temp were another type then "A a = temp;" would mean something
different).
2.
A a = A(b);

In this case the compiler is free to either:
- create a temporary A(b) and then use the copy ctor to initialize "a" from it
- directly initialize a from "b" like you would have written "A a(b);"

However, in BOTH cases the compiler still has to require a copy ctor
of the form "A(A const&)".
the first works as expected
for the 2nd, compiler complains that there's no function A:A(A).

Is it my any chance that "A" is auto_ptr? (or a similar type that has
"A(A&)" copy ctor but does not have "A(A const&)" version?) Because
then it can be explained why the first compiles while the other
doesn't. If you have a "A(A&)" only copy ctor then in the first case
you are using that because you initialize "a" from a "A" lvalue (and a
reference "A&" can be bound to it). In the second case however you try
to initialize "a" from a temporary (even as I said if the compiler
doesn't use the temporary and copy ctor and directly tries to
initialize "a" from "b" it still requires a "A(A const&)" copy ctor
"as if" it would have created a temporary). And because you can't bind
reference to non const (ie "A&") to temporaries then you get the
error.
I don't get it. IMHO, they should be the same.

Your code sample is incorrect and incomplete, I'm just guessing here.
 
T

tony_in_da_uk

what's the difference between these two statements?

1.
class A{};
A temp(b); A a = temp;

2.
A a = A(b);

the first works as expected
for the 2nd, compiler complains that there's no function A:A(A).

I don't get it. IMHO, they should be the same.

In 1, A temp(b) indicates the existence of something akin to
A::A([const] typeof(b)[&]). A a = temp === A a(temp) and implies
A::A([const] A[&]).

In 2, A a = A(b) is equivalent to A a(A(b)); where A(b) creates a
temporary. So perhaps you only have a copy constructor defined for
non-const lvalues...?

Tony
 
T

thomas

This won't compile as you try to initialize A with a ctor that would
take a single argument and you have no such ctor defined.


Initialization as if you would have written instead "A a(temp);"
(notice that this is true ONLY because temp is the same type as A, if
temp were another type then "A a = temp;" would mean something
different).


In this case the compiler is free to either:
- create a temporary A(b) and then use the copy ctor to initialize "a" from it
- directly initialize a from "b" like you would have written "A a(b);"

However, in BOTH cases the compiler still has to require a copy ctor
of the form "A(A const&)".


Is it my any chance that "A" is auto_ptr? (or a similar type that has
"A(A&)" copy ctor but does not have "A(A const&)" version?) Because
then it can be explained why the first compiles while the other
doesn't. If you have a "A(A&)" only copy ctor then in the first case
you are using that because you initialize "a" from a "A" lvalue (and a
reference "A&" can be bound to it). In the second case however you try
to initialize "a" from a temporary (even as I said if the compiler
doesn't use the temporary and copy ctor and directly tries to
initialize "a" from "b" it still requires a "A(A const&)" copy ctor
"as if" it would have created a temporary). And because you can't bind
reference to non const (ie "A&") to temporaries then you get the
error.


Your code sample is incorrect and incomplete, I'm just guessing here.

ofz, that I have defined a copy constructor in my class, although not
shown explicitly.
But the point rather matters is that you cannot take a reference of a
temporary object.
Thank you guys for kindly help.
 
D

dizzy

thomas said:
ofz, that I have defined a copy constructor in my class, although
not shown explicitly.

When you ask questions about why something happens in some way you
should provide all relevant code parts. You didn't.
But the point rather matters is that you cannot take a reference of
a temporary object.

You can bind references to const to temporary objects but not
references to non const.
 
T

thomas

When you ask questions about why something happens in some way you
should provide all relevant code parts. You didn't.
yeah.. I will change my bad habit.
You can bind references to const to temporary objects but not
references to non const.
I think a reference is an alias of an object, so if the object is
gone, the reference will cry.
Whether the object will exist during the reference usage should be the
only rule to judge whether the reference is used correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,304
Latest member
CalmwellCBDIngredients

Latest Threads

Top