free function or member function for overloading += operator?

Discussion in 'C++' started by PengYu.UT@gmail.com, Mar 13, 2007.

  1. Guest

    Hi,

    I have the following two programs. One is to overload += as a free
    function, the other is to overload += as a member function. Both of
    them works fine.

    I'm wondering which one I should choose in practice. What are the pros
    and cons for these two implementations?

    Thanks,
    Peng


    $ cat main_free.cc
    #include <iostream>

    class Integer {
    public:
    Integer(int i) : _i(i) {}
    friend Integer &operator+=(Integer &, int);
    friend std::eek:stream &operator<<(std::eek:stream &, const Integer &);
    private:
    int _i;
    };

    Integer & operator+=(Integer & I, int i) {
    I._i += i;
    return I;
    }

    std::eek:stream &operator<<(std::eek:stream &os, const Integer &I) {
    os << "Integer: " << I._i;
    return os;
    }

    int main ()
    {
    Integer I(0);
    std::cout << I << std::endl;
    I += 10;
    std::cout << I << std::endl;
    }


    $ cat main_member.cc
    #include <iostream>

    class Integer {
    public:
    Integer(int i) : _i(i) {}
    Integer &operator+=(int i) {
    _i += i;
    return *this;
    }
    friend std::eek:stream &operator<<(std::eek:stream &, const Integer &);
    private:
    int _i;
    };


    std::eek:stream &operator<<(std::eek:stream &os, const Integer &I) {
    os << "Integer: " << I._i;
    }

    int main ()
    {
    Integer I(0);
    std::cout << I << std::endl;
    I += 10;
    std::cout << I << std::endl;
    }


    $ ./main_free.exe
    Integer: 0
    Integer: 10
    $ ./main_member.exe
    Integer: 0
    Integer: 10
    , Mar 13, 2007
    #1
    1. Advertising

  2. wrote:
    > I have the following two programs. One is to overload += as a free
    > function, the other is to overload += as a member function. Both of
    > them works fine.
    >
    > I'm wondering which one I should choose in practice. What are the pros
    > and cons for these two implementations?
    > [..]


    A general rule: if the operator modifies the object, it should be
    implemented as a member. I have no comment beyond that.

    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, Mar 13, 2007
    #2
    1. Advertising

  3. Marcus Kwok Guest

    <> wrote:
    > Hi,
    >
    > I have the following two programs. One is to overload += as a free
    > function, the other is to overload += as a member function. Both of
    > them works fine.
    >
    > I'm wondering which one I should choose in practice. What are the pros
    > and cons for these two implementations?


    One common idiom is to implement += as a member, and + as a free
    function written in terms of +=. For example (untested):

    Integer& Integer::eek:perator+=(const Integer& rhs)
    {
    _i += rhs._i;
    return *this;
    }

    Integer operator+(const Integer& lhs, const Integer& rhs)
    {
    Integer temp(lhs);
    lhs += rhs;
    return temp;
    }

    I seem to remember that this idiom allows for conversions to take place
    on the left operands for operator+. I'm not sure if there are any other
    technical advantages to this approach though.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Mar 14, 2007
    #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. Thomas
    Replies:
    3
    Views:
    451
    DaKoadMunky
    Sep 21, 2004
  2. =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

    operator==, member function or free function

    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Apr 7, 2005, in forum: C++
    Replies:
    6
    Views:
    2,295
    Donovan Rebbechi
    Apr 7, 2005
  3. Replies:
    11
    Views:
    727
    James Kanze
    May 16, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    414
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page