Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > free function or member function for overloading += operator?

Reply
Thread Tools

free function or member function for overloading += operator?

 
 
PengYu.UT@gmail.com
Guest
Posts: n/a
 
      03-13-2007
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:stream &operator<<(std:stream &, const Integer &);
private:
int _i;
};

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

std:stream &operator<<(std: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:stream &operator<<(std:stream &, const Integer &);
private:
int _i;
};


std:stream &operator<<(std: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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      03-14-2007
(E-Mail Removed) <(E-Mail Removed)> 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: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
 
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
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments