some more about operator overloading

Discussion in 'C++' started by Tony Johansson, May 23, 2005.

  1. Hello!

    I have this wrapper class Integer below that I use when testing operator
    overloading.
    A book that I read say that the expression
    Integer i;
    i+5 is translated to operator+(i,5) using the stand-alone helper function.
    If Integer's constructor were not declared as explicit, the previous would
    have one more possible translation which is
    operator+(i, Integer(5))
    for which the constructor would first convert the value 5 to the Integer
    type, and then two object of type Integer would be added. Therefore the code
    would have an ambiguous translation, and the compiler would refuse to
    compile it.With the explicit specification, the latter possibility does not
    apply, and the code compiles.

    Now to my first question.Wwhy would the code have ambiguous translation?
    My second question. Why does it works perfactly without this specification
    explicit?
    My third question in what cases would I get this mentioned about ambiguous
    translation?

    int main()
    {
    Integer i(2);
    Integer k = i+5;
    cout << k.get();
    return 0;
    }

    class Integer
    {
    public:
    Integer (int i = 0 : value_(i)
    {}

    int get() const
    { return value_; }

    Integer operator+(const Integer& i) const
    {
    Integer local(value_ + i.value_);
    return local;
    }

    friend Integer operator+(int v, const Integer& i)
    {
    Integer local(v + i.value_);
    return local;
    }

    private:
    int value_;
    };

    Integer operator+(const Integer& i, int v)
    {
    Integer local(v + i.get());
    return local;
    }

    //Tony
     
    Tony Johansson, May 23, 2005
    #1
    1. Advertising

  2. Tony Johansson wrote:
    >
    > Hello!
    >
    > I have this wrapper class Integer below that I use when testing operator
    > overloading.
    > A book that I read say that the expression
    > Integer i;
    > i+5 is translated to operator+(i,5) using the stand-alone helper function.


    right

    > If Integer's constructor were not declared as explicit,


    it isn't

    > the previous would
    > have one more possible translation which is
    > operator+(i, Integer(5))
    > for which the constructor would first convert the value 5 to the Integer
    > type, and then two object of type Integer would be added.


    right. Using the member function operator+

    Therefore the code
    > would have an ambiguous translation, and the compiler would refuse to
    > compile it.


    Wrong.
    Using the standalone function no object creation is necessary, thus this
    is considered to be a better match then using the member function. The
    compiler would also choose this one.

    > With the explicit specification, the latter possibility does not
    > apply, and the code compiles.


    There is no 'explicit' in the posted code.

    >
    > Now to my first question.Wwhy would the code have ambiguous translation?


    There is no ambiguity in the posted code.

    > My second question. Why does it works perfactly without this specification
    > explicit?


    Because for using the standalone function, no conversions are needed. Thus this
    would be a perfect match. Since there is no other 'perfect match' possible, no
    ambiguity arises.

    > My third question in what cases would I get this mentioned about ambiguous
    > translation?
    >


    eg.

    #include <iostream>
    using namespace std;

    class Integer
    {
    public:
    Integer (int i = 0 ) : value_(i)
    {}

    int get() const
    { return value_; }

    Integer operator+(const Integer& i) const
    {
    Integer local(value_ + i.value_);
    return local;
    }

    friend Integer operator+( const Integer& i, const Integer& v)
    {
    Integer local(v.value_ + i.value_);
    return local;
    }

    private:
    int value_;
    };

    int main()
    {
    Integer i(2);
    Integer k = i+5;
    cout << k.get();
    return 0;
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, May 23, 2005
    #2
    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. John Smith
    Replies:
    2
    Views:
    449
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    767
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    756
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    584
  5. Replies:
    2
    Views:
    335
Loading...

Share This Page