Conversion constructor

T

Taras_96

Hi,

In another thread, http://tiny.cc/9sVqr, it was written that:

"By definition, the form

TypeA objA = objB;

is a shorthand for

TypeA objA((TypeA(objB)));"

Why isn't the above short hand for:

TypeA objA(objB); ?

What if I had actually written

TypeA objA(objB);

in the source?

Taras
 
A

Andrey Tarasevich

Taras_96 said:
In another thread, http://tiny.cc/9sVqr, it was written that:

"By definition, the form

TypeA objA = objB;

is a shorthand for

TypeA objA((TypeA(objB)));"

Yes, unless 'objB' is originally of type 'TypeA' (ignoring
cv-qualifications). In the latter case it is equivalent to just

TypeA objA(objB);
Why isn't the above short hand for:

TypeA objA(objB); ?

Why should it be? If you want the latter, you can just write it explicitly.

The copy-initialization form (with '=') is more restrictive - it always
uses the copy-constructor (at least conceptually). The
direct-initialization form is less restrictive - it can use any
conversion constructor. This sometimes can be useful when resolving
ambiguous constructor overloads. I.e. copy-initialization might be
well-formed when direct-initialization is ill-formed because of overload
ambiguity.
What if I had actually written

TypeA objA(objB);

in the source?

So? Then you'd just have direct-initialization instead of
copy-initialization.
 
T

Taras_96

Yes, unless 'objB' is originally of type 'TypeA' (ignoring
cv-qualifications). In the latter case it is equivalent to just

   TypeA objA(objB);



Why should it be? If you want the latter, you can just write it explicitly.

The copy-initialization form (with '=') is more restrictive - it always
uses the copy-constructor (at least conceptually). The
direct-initialization form is less restrictive - it can use any
conversion constructor. This sometimes can be useful when resolving
ambiguous constructor overloads. I.e. copy-initialization might be
well-formed when direct-initialization is ill-formed because of overload
ambiguity.




So? Then you'd just have direct-initialization instead of
copy-initialization.

Thanks Andrey
 
T

Taras_96

Yes, unless 'objB' is originally of type 'TypeA' (ignoring
cv-qualifications). In the latter case it is equivalent to just

   TypeA objA(objB);



Why should it be? If you want the latter, you can just write it explicitly.

The copy-initialization form (with '=') is more restrictive - it always
uses the copy-constructor (at least conceptually). The
direct-initialization form is less restrictive - it can use any
conversion constructor. This sometimes can be useful when resolving
ambiguous constructor overloads. I.e. copy-initialization might be
well-formed when direct-initialization is ill-formed because of overload
ambiguity.




So? Then you'd just have direct-initialization instead of
copy-initialization.

Thanks Andrey
 

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

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top