automatic type conversion

Discussion in 'C++' started by Rahul, Dec 11, 2007.

  1. Rahul

    Rahul Guest

    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::eek: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?
     
    Rahul, Dec 11, 2007
    #1
    1. Advertising

  2. 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::eek: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::eek: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
     
    Victor Bazarov, Dec 11, 2007
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Dec 11, 11:33 pm, "Victor Bazarov" <> 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::eek: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::eek: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?
     
    Rahul, Dec 11, 2007
    #3
  4. Rahul

    red floyd Guest

    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. posted the exact same question.

    To vairavans and Rahul, please see FAQ 5.2:
    www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
     
    red floyd, Dec 11, 2007
    #4
  5. Rahul

    Rahul Guest

    On Dec 12, 12:14 am, red floyd <> 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. posted the exact same question.
    >
    > To vairavans and Rahul, please see FAQ 5.2:www.parashift.com/c -faq-lite/how-to-post.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...
     
    Rahul, Dec 11, 2007
    #5
  6. Rahul

    red floyd Guest

    Rahul wrote:
    > On Dec 12, 12:14 am, red floyd <> 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. posted the exact same question.
    >>
    >> To vairavans and Rahul, please see FAQ 5.2:www.parashift.com/c -faq-lite/how-to-post.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?
     
    red floyd, Dec 11, 2007
    #6
  7. Rahul

    James Kanze Guest

    On Dec 11, 7:28 pm, Rahul <> 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::eek: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::eek: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::eek:perator
    B() can no longer be called on obj. (You'll also have to
    provide a user defined default constructor for A.)

    Declare A::eek: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:
    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
     
    James Kanze, Dec 12, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Heyes

    Automatic type conversion.

    Jason Heyes, Nov 28, 2004, in forum: C++
    Replies:
    4
    Views:
    3,258
    Peter Koch Larsen
    Nov 29, 2004
  2. Szabolcs
    Replies:
    3
    Views:
    740
    Gianni Mariani
    Jun 11, 2007
  3. Russ
    Replies:
    3
    Views:
    307
    Dan Bishop
    Jul 27, 2007
  4. MWimmer
    Replies:
    0
    Views:
    298
    MWimmer
    Sep 27, 2007
  5. Bruce Eckel

    Automatic Type Conversion to String

    Bruce Eckel, Feb 13, 2012, in forum: Python
    Replies:
    6
    Views:
    190
    Ned Deily
    Feb 15, 2012
Loading...

Share This Page