Wednesday, February 20, 2008

Friend function in C++

The Friend functions are functions defined outside a class (i.e., not member functions), but which the class declares to be friends so that they can make use of the class's private members. This is normally used in operator overloading. Perhaps the most general use of friend functions is overloading <<>> for I/O.

Another use of friend functions is to allow operators to be commutative. An operator is commutative if the end result is the same in spite of the order of its operands. Some typical examples are addition and multiplication. But, Subtraction and division are not commutative.

No comments: