friend func in cpp
friend func in cpp
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
Consider a class Foo. The standard syntax for writing an object to a stream is the << operator:
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:
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.
Code: Select all
ostream & operator<<( ostream & os, const Foo & f );
...
Foo f( "bar" );
cout << f << endl;
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;
}
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.
Re:friend func in cpp
Shouldn't this be:ostream & operator<<( ostream & os, const Foo & f )
{
os << f.baz;
}
Code: Select all
ostream & operator<<( ostream & os, const Foo & f )
{
return os << f.baz;
}
Re:friend func in cpp
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.
Re:friend func in cpp
well thanks that made it quite clear
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??
i went through a book bout this toipc and came across that this func could be used as a bridge........Solar wrote: That being said, other than for the stuff described above, don't use friends.
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
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.rich_m wrote: well thanks that made it quite clear
i went through a book bout this toipc and came across that this func could be used as a bridge........Solar wrote: That being said, other than for the stuff described above, don't use friends.
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??
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
so that means the friend func has to be used in such kinda programs, right?
Re:friend func in cpp
Could you be more precise in what you're asking?
Yes, there are valid uses for "friend", but not many.
Yes, there are valid uses for "friend", but not many.
Every good solution is obvious once you've found it.
Re:friend func in cpp
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.Guest wrote: so that means the friend func has to be used in such kinda programs, right?