You are here:

C++/freind functions

Advertisement


Question
Hi,

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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.