C++/freind functions
Expert: vijayan - 11/22/2009
QuestionHi,
i have some basic doubt. i have seen some example programms on operator overloading. why operator overloading function will declare as friend functions. can you tell if there is any relation of operator overloading and freind functions? is there any scenarios in opeartor overloading that i have to declare as freind functions ?
as per my knowledge i know only to access private data of class .
Many thanks in advance,
Siva
Answer> can you tell if there is any relation of operator overloading and freind functions?
Overloading an operator using a non-member function and declaring a non-member functions as a friend are two different, orthogonal concepts. A non-member function overloading an operator may or may not be declared as a friend.
> is there any scenarios in opeartor overloading that i have to declare as freind functions ?
Friend functions have the same access to members of the class as member functions. If (and only if) the non-member function overloading an operator requires access rights that normal functions do not have - for example, it needs to access private members of the class - it should be declared a friend.