Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > automatic type conversion

Reply
Thread Tools

automatic type conversion

 
 
Rahul
Guest
Posts: n/a
 
      12-11-2007
Hi Everyone,

I have the following code,


class B;

class A
{
public : operator B();
};

class B
{
public : B()
{
}
B(const A& ref)
{
printf("destination conversion\n");
}

B(const B& ref)
{
printf("copy constructor\n");
}
};

A:perator B()
{
printf("source conversion\n");
return B();
}
int main()
{
A obj;
B obj1 = obj;
return(0);
}

so the output is

destination conversion.

There are actually two ways to convert from obj to obj1
1) destination conversion, constructor function of B
2) source conversion, operator function of A and then copy constructor
of B

The compiler has selected the first option, where as i expected a
ambiguity compile time error, what does the standard say for this?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-11-2007
Rahul wrote:
> Hi Everyone,
>
> I have the following code,
>
>
> class B;
>
> class A
> {
> public : operator B();
> };
>
> class B
> {
> public : B()
> {
> }
> B(const A& ref)
> {
> printf("destination conversion\n");
> }
>
> B(const B& ref)
> {
> printf("copy constructor\n");
> }
> };
>
> A:perator B()
> {
> printf("source conversion\n");
> return B();
> }
> int main()
> {
> A obj;
> B obj1 = obj;
> return(0);
> }
>
> so the output is
>
> destination conversion.
>
> There are actually two ways to convert from obj to obj1
> 1) destination conversion, constructor function of B
> 2) source conversion, operator function of A and then copy constructor
> of B
>
> The compiler has selected the first option, where as i expected a
> ambiguity compile time error, what does the standard say for this?


Your declaration/definition/initialisation statement

B obj1 = obj;

is actually another form of writing

B obj1((B(obj)));

(and the copying from the temporary of type B to 'obj1' is allowed
to be optimized away by the compiler). As you can see, the temporary
is created from 'obj'. There are two ways to create it: by means of
the converting constructor in B or by means of creating *another*
temporary from A:perator B() and then copy-constructing the original
temporary. The compiler chooses the short way, I guess.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      12-11-2007
On Dec 11, 11:33 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Rahul wrote:
> > Hi Everyone,

>
> > I have the following code,

>
> > class B;

>
> > class A
> > {
> > public : operator B();
> > };

>
> > class B
> > {
> > public : B()
> > {
> > }
> > B(const A& ref)
> > {
> > printf("destination conversion\n");
> > }

>
> > B(const B& ref)
> > {
> > printf("copy constructor\n");
> > }
> > };

>
> > A:perator B()
> > {
> > printf("source conversion\n");
> > return B();
> > }
> > int main()
> > {
> > A obj;
> > B obj1 = obj;
> > return(0);
> > }

>
> > so the output is

>
> > destination conversion.

>
> > There are actually two ways to convert from obj to obj1
> > 1) destination conversion, constructor function of B
> > 2) source conversion, operator function of A and then copy constructor
> > of B

>
> > The compiler has selected the first option, where as i expected a
> > ambiguity compile time error, what does the standard say for this?

>
> Your declaration/definition/initialisation statement
>
> B obj1 = obj;
>
> is actually another form of writing
>
> B obj1((B(obj)));
>
> (and the copying from the temporary of type B to 'obj1' is allowed
> to be optimized away by the compiler). As you can see, the temporary
> is created from 'obj'. There are two ways to create it: by means of
> the converting constructor in B or by means of creating *another*
> temporary from A:perator B() and then copy-constructing the original
> temporary. The compiler chooses the short way, I guess.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


But the moment i have the following code,

A obj;
B obj1;
obj1 = obj;

I get an error, and the compiler is not able to decide between the
constructor conversion and the operator conversion... So i expected a
similar error in the initial posted code... Does anyone know what the
standard says for these cases?
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-11-2007
Rahul wrote:
>
> class B;
>
> class A
> {
> public : operator B();
> };
>
> class B
> {
> public : B()
> {
> }
> B(const A& ref)
> {
> printf("destination conversion\n");
> }
> [remainder redactged]


This is homework. http://www.velocityreviews.com/forums/(E-Mail Removed) posted the exact same question.

To vairavans and Rahul, please see FAQ 5.2:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.2


 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      12-11-2007
On Dec 12, 12:14 am, red floyd <(E-Mail Removed)> wrote:
> Rahul wrote:
>
> > class B;

>
> > class A
> > {
> > public : operator B();
> > };

>
> > class B
> > {
> > public : B()
> > {
> > }
> > B(const A& ref)
> > {
> > printf("destination conversion\n");
> > }
> > [remainder redactged]

>
> This is homework. (E-Mail Removed) posted the exact same question.
>
> To vairavans and Rahul, please see FAQ 5.2:http://www.parashift.com/c++-faq-lit...t.html#faq-5.2


I don't understand what possible home work do u make out of it?
I'm just trying to understand cases where compiler spots an
ambiguity... besides the complexity just increases with operator=,
copy constructor and their combination with source converter or
destination converter or both...
Naturally one would like to clear out the basic simple cases
first...
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-11-2007
Rahul wrote:
> On Dec 12, 12:14 am, red floyd <(E-Mail Removed)> wrote:
>> Rahul wrote:
>>
>>> class B;
>>> class A
>>> {
>>> public : operator B();
>>> };
>>> class B
>>> {
>>> public : B()
>>> {
>>> }
>>> B(const A& ref)
>>> {
>>> printf("destination conversion\n");
>>> }
>>> [remainder redactged]

>> This is homework. (E-Mail Removed) posted the exact same question.
>>
>> To vairavans and Rahul, please see FAQ 5.2:http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

>
> I don't understand what possible home work do u make out of it?
> I'm just trying to understand cases where compiler spots an
> ambiguity... besides the complexity just increases with operator=,
> copy constructor and their combination with source converter or
> destination converter or both...
> Naturally one would like to clear out the basic simple cases
> first...



If it's not homework, then why did you and vairavans post identical code
(down to formatting!) with the same question?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-12-2007
On Dec 11, 7:28 pm, Rahul <(E-Mail Removed)> wrote:

> I have the following code,


> class B;


> class A
> {
> public : operator B();
> };


> class B
> {
> public : B()
> {
> }
> B(const A& ref)
> {
> printf("destination conversion\n");
> }


> B(const B& ref)
> {
> printf("copy constructor\n");
> }
> };


> A:perator B()
> {
> printf("source conversion\n");
> return B();}


> int main()
> {
> A obj;
> B obj1 = obj;
> return(0);
> }


> so the output is


> destination conversion.


> There are actually two ways to convert from obj to obj1
> 1) destination conversion, constructor function of B
> 2) source conversion, operator function of A and then copy constructor
> of B


> The compiler has selected the first option, where as I
> expected a ambiguity compile time error, what does the
> standard say for this?


That you should see "source conversion" (which is also what I
get from g++ and Sun CC). The requirement in "B obj1 = obj;" is
that obj must be converted into a B (which is then used as an
argument for the copy constructor). A:perator B() is an exact
match, in every detail. B( A const& ) is classified as an exact
match, but does require adding a const, which in this case, the
compiler shoul use as a tie-breaker.

Change the code to:
A const obj ;
B obj1 = obj ;
and you should see "destination conversion", since A:perator
B() can no longer be called on obj. (You'll also have to
provide a user defined default constructor for A.)

Declare A:perator B() const (which it probablyl should be),
and the conversions will be ambiguous (but g++ allows it, using
"destination conversions"---an error in g++).

In practice, of course, it shouldn't matter---you should never
provide round-trip implicit conversions anyway.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Type Conversion to String Bruce Eckel Python 6 02-15-2012 06:21 PM
Automatic type conversion to complex<T>? MWimmer C++ 0 09-27-2007 02:16 PM
automatic type conversion for comparison operators Russ Python 3 07-27-2007 04:13 AM
Templates & automatic type conversion Szabolcs C++ 3 06-11-2007 01:16 PM
Automatic type conversion. Jason Heyes C++ 4 11-29-2004 12:09 AM



Advertisments