GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2007, 18:03
meili100 meili100 is offline
Junior Member
 
Join Date: Feb 2007
Posts: 37
meili100 has a little shameless behaviour in the past

What's good to implement operator overloading with 'friend'?


We all know how to implement operator overloading as the following:

CPP / C++ / C Code:
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
complex operator +(const complex & c);
};

But sometimes I see people use friend methods:


CPP / C++ / C Code:
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
friend complex operator +(const complex &c1, const complex &c2);
};


Why do they do this way? What're the advantages?
Thank you.
  #2  
Old 08-May-2007, 09:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,619
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: What's good to implement operator overloading with 'friend'?


Quote:
Originally Posted by meili100
operator overloading as the following:

CPP / C++ / C Code:
class complex
{
public:
complex(double r, double i){
real = r, imag = i;}
complex operator +(const complex & c);
};

Usually the data members real and imag are private to the class.

The above function overloads the '+' operator for the complex class so that we can write z = x + y for complex objects. It operates on two objects of the class and the result is a member of the class. Since it is being defined as a member function of the class, it has access to all members (public and private) of the class. As you imply, this is pretty much standard stuff.

Quote:
Originally Posted by meili100
But sometimes I see ...


CPP / C++ / C Code:
class complex
{
public:
.

friend complex operator +(const complex &c1, const complex &c2);
};


Why do they do this way?
I don't have a clue.
Quote:
Originally Posted by meili100
What're the advantages?
You must ask the people who defined it this way. There is always a reason that people do things. (That doesn't mean that there's always a goodreason. However, just because I don't know why someone did something doesn't mean that he/she didn't have a good reason. So: ask them.)

Friend operators defined in a class will allow access to private members from other classes. Since this operates on two complex objects and gives a complex object, I'm not sure who could be a friend.

Now a good reason to define a function as a friend be if you wanted to overload the '<<' operator so that an ostream object could access the private members of the complex class in order to print them out.

CPP / C++ / C Code:
  friend std::ostream& operator <<(std::ostream &out, const complex& num);

Regards,

Dave
  #3  
Old 22-Jul-2007, 07:34
surya.k surya.k is offline
New Member
 
Join Date: Aug 2006
Posts: 14
surya.k is on a distinguished road

Re: What's good to implement operator overloading with 'friend'?


hai meili, they done this prg in this way becuase...........
while u pass any arguments that are not modified in the function(the data members of an object), it is always better to send them as const objects which enables faster compilation,so they done so....
n why they use friend fun is,u want to store the result of the two complex numbers in the third complex no., the calling of the function (overloaded operator) is interpreted by the compiler in the following way........
complex1+complex2; //in compiler-> operator +(complex1,complex2);
so in order to store the result in the third complex object it should return the reference to the resultant complex,n if u use class member fun, it is not possible to return the reference as the compiler under stands the call as follows
complex1+complex2
//in compiler it is treated as complex1.operator +(complex2);
so we need frend fun.....
thats wht dave explained u.....
regards
surya
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please recommend a good, user-friendly website builder dreambuilder Web Design Forum 11 23-Aug-2007 15:53
Help with creating files in c molatelo C Programming Language 1 28-Apr-2007 07:20
friend functions error CaptnB CPP / C++ Forum 2 12-Jun-2006 13:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:13.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.