Page 1 of 1
friend func in cpp
Posted: Thu Jan 27, 2005 11:07 am
by rich_m
i've just learnt about this so called friend function and from what i understand it's a function which can access all objests of a class where it is a member . I'm still unable to get a clear picture of the purpose of such a function ??? . My proff has asked us to create simple programs using it, could anyone enlighten me bout this function , so that i could get some ideas.
Re:friend func in cpp
Posted: Thu Jan 27, 2005 2:30 pm
by Solar
Consider a class Foo. The standard syntax for writing an object to a stream is the << operator:
Code: Select all
ostream & operator<<( ostream & os, const Foo & f );
...
Foo f( "bar" );
cout << f << endl;
You can't make operator<<() a member of Foo, however, since that would require you to call f.operator<<( cout, f ) instead of the syntax above. Your Foo's operator<<() must not be a member function, but it still needs access to Foo's private members in order to print them to cout.
Enter friend:
Code: Select all
class Foo
{
public:
...
friend ostream & operator<<( ostream & os, const Foo &f );
private:
int baz;
};
ostream & operator<<( ostream & os, const Foo & f )
{
return os << f.baz;
}
Voila, f.baz can be printed to cout by a function that's not member of Foo.
That being said, other than for the stuff described above, don't use friends. In virtually every case they seem like a good design idea, they'll bite you later. They are an object-oriented [tt]goto[/tt], so to speak: Sometimes unavoidable, but always ugly.
Re:friend func in cpp
Posted: Fri Jan 28, 2005 1:04 am
by AR
ostream & operator<<( ostream & os, const Foo & f )
{
os << f.baz;
}
Shouldn't this be:
Code: Select all
ostream & operator<<( ostream & os, const Foo & f )
{
return os << f.baz;
}
Re:friend func in cpp
Posted: Fri Jan 28, 2005 9:08 am
by Solar
Yes, you're right of course. (See? Never type code without thinking, or while going to bed.
)
Re:friend func in cpp
Posted: Sun Jan 30, 2005 12:26 pm
by rich_m
well thanks that made it quite clear
Solar wrote:
That being said, other than for the stuff described above, don't use friends.
i went through a book bout this toipc and came across that this func could be used as a bridge........
i.e. bridging data from two different classes. now lets say they are two classes "euros" and "dollars" , now i need a function that would add the value of these 2 and return it in euros. would this be an ideal example of the use of friend or is there an alternative??
Re:friend func in cpp
Posted: Mon Jan 31, 2005 2:02 am
by Candy
rich_m wrote:
well thanks that made it quite clear
Solar wrote:
That being said, other than for the stuff described above, don't use friends.
i went through a book bout this toipc and came across that this func could be used as a bridge........
i.e. bridging data from two different classes. now lets say they are two classes "euros" and "dollars" , now i need a function that would add the value of these 2 and return it in euros. would this be an ideal example of the use of friend or is there an alternative??
If you're defining one of the types, give it an operator for the other type. More or less define a "default" money type and relate the rest to it.
Or, make the exchange rates a different thing, and make a weighted graph with exchange rates. Although, for a money converter it might just be overkill
Re:friend func in cpp
Posted: Thu Feb 03, 2005 11:32 pm
by Guest
so that means the friend func has to be used in such kinda programs, right?
Re:friend func in cpp
Posted: Fri Feb 04, 2005 1:52 am
by Solar
Could you be more precise in what you're asking?
Yes, there are valid uses for "friend", but not many.
Re:friend func in cpp
Posted: Fri Feb 04, 2005 6:53 am
by Candy
Guest wrote:
so that means the friend func has to be used in such kinda programs, right?
You do not need friends for all operator types. Just for those that should be in a different class, but needs internal access to your class too. Say, things on ostreams etc, you can't do that with them.