friend func in cpp

Programming, for all ages and all languages.
Post Reply
rich_m

friend func in cpp

Post 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. :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:friend func in cpp

Post 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.
Every good solution is obvious once you've found it.
AR

Re:friend func in cpp

Post 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;
}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:friend func in cpp

Post by Solar »

Yes, you're right of course. (See? Never type code without thinking, or while going to bed. ;) )
Every good solution is obvious once you've found it.
rich_m

Re:friend func in cpp

Post 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??
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:friend func in cpp

Post 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 ;)
Guest

Re:friend func in cpp

Post by Guest »

so that means the friend func has to be used in such kinda programs, right?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:friend func in cpp

Post by Solar »

Could you be more precise in what you're asking?

Yes, there are valid uses for "friend", but not many.
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:friend func in cpp

Post 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.
Post Reply