conversion operator and conversion ctor

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Sep 15, 2009.

  1. , India

    , India Guest

    Consider the following program named x.cpp

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    class First;

    class Second
    {
    public:
    Second();
    Second(const First& arg);
    int getValue(void) const;

    private:
    int val;
    };

    class First
    {
    public:
    First(int arg = -1);
    int getValue(void) const;
    operator Second(void);

    private:
    int val;
    };

    Second::Second() : val(-1000)
    {
    cout << "Second::default ctor called. val = " << val << endl;
    }

    Second::Second(const First& arg) : val(arg.getValue())
    {
    cout << "Second:: one argument ctor called. val = " << val <<
    endl;
    }

    inline int Second::getValue(void) const
    {
    return val;
    }

    inline First::First(int arg) : val(arg)
    {
    }

    inline int First::getValue(void) const
    {
    return val;
    }

    inline First::eek:perator Second(void)
    {
    cout << "First::eek:perator Second() called" << endl;
    return Second();
    }

    void print(const Second& obj)
    {
    cout << "From print(): " << obj.getValue() << endl;
    return;
    }

    int main()
    {
    First obj(500);

    print(obj);

    return EXIT_SUCCESS;
    }

    This program compiles fine with g++3.4.3 as
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
    and produced the output
    First::eek:perator Second() called
    Second::default ctor called. val = -1000
    From print(): -1000

    I have the following question(for learning purpose only)
    the print() function takes a 'Second' type object and I am passing a
    'First' type object. From 'First' type to 'Second' type, there are two
    possibilities namely,
    First::eek:perator Second(void)
    and
    Second::Second(const First& arg)
    Am I correct ? If so, then why doesn't the compiler give ambiguity
    error for the two possibilities but instead picks up First::eek:perator
    Second() ?

    I am unable to understand this. Kindly clarify this. What does the
    Standard say regarding which one among these two functions to be
    chosen ?

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Sep 15, 2009
    #1
    1. Advertising

  2. , India

    James Kanze Guest

    On Sep 15, 9:23 am, ", India"
    <> wrote:
    > Consider the following program named x.cpp


    > #include <cstdlib>
    > #include <iostream>


    > using namespace std;


    > class First;


    > class Second
    > {
    > public:
    > Second();
    > Second(const First& arg);
    > int getValue(void) const;


    > private:
    > int val;
    > };


    > class First
    > {
    > public:
    > First(int arg = -1);
    > int getValue(void) const;
    > operator Second(void);


    Shouldn't this function be const? It doesn't change the value
    of First.

    > private:
    > int val;
    > };


    > Second::Second() : val(-1000)
    > {
    > cout << "Second::default ctor called. val = " << val << endl;
    > }


    > Second::Second(const First& arg) : val(arg.getValue())
    > {
    > cout << "Second:: one argument ctor called. val = " << val <<
    > endl;
    > }


    > inline int Second::getValue(void) const
    > {
    > return val;
    > }


    > inline First::First(int arg) : val(arg)
    > {
    > }


    > inline int First::getValue(void) const
    > {
    > return val;
    > }


    > inline First::eek:perator Second(void)
    > {
    > cout << "First::eek:perator Second() called" << endl;
    > return Second();
    > }


    > void print(const Second& obj)
    > {
    > cout << "From print(): " << obj.getValue() << endl;
    > return;
    > }


    > int main()
    > {
    > First obj(500);
    > print(obj);
    > return EXIT_SUCCESS;
    > }


    > This program compiles fine with g++3.4.3 as
    > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
    > and produced the output
    > First::eek:perator Second() called
    > Second::default ctor called. val = -1000
    > From print(): -1000


    > I have the following question(for learning purpose only) the
    > print() function takes a 'Second' type object and I am passing
    > a 'First' type object.


    > From 'First' type to 'Second' type,
    > there are two possibilities namely,
    > First::eek:perator Second(void)
    > and
    > Second::Second(const First& arg)
    > Am I correct ?


    The first only works if the expression to be converted is a
    non-const.

    > If so, then why doesn't the compiler give ambiguity error for
    > the two possibilities but instead picks up First::eek:perator
    > Second() ?


    For the same reasons the compiler generally prefers a non-const
    function over a const--- in this case, First::eek:perator Second()
    is an exact match, and Second::Second( First const& ) requires a
    const conversion. In general, given a non-const lvalue, the
    compiler will prefer a function which treats the object as a
    non-const to one which treats it as const.

    > I am unable to understand this. Kindly clarify this. What does
    > the Standard say regarding which one among these two functions
    > to be chosen ?


    That First::eek:perator Second() should be preferred for non-const
    values, and Second::Second( First const& ) for const values.

    --
    James Kanze
    James Kanze, Sep 15, 2009
    #2
    1. Advertising

  3. , India

    Fraser Ross Guest

    The standard has unusual overload resolution rules for this and has the
    term "ambiguous conversion sequence" for it. Have a look at
    13.3.3.1/10.

    Fraser.
    Fraser Ross, Sep 15, 2009
    #3
    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. Apricot
    Replies:
    4
    Views:
    504
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    473
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    456
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    394
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    410
    James Kanze
    Apr 15, 2008
Loading...

Share This Page