Should my class declare a friend function or member function, C/C++ Programming

A: Use a member while you can and a friend when you need to.

Sometimes friends are better syntactically (e.g., in class Fred, friend functions let the Fred parameter to be second, whereas members require it to be first). Another good employ of friend functions are the binary infix arithmetic operators. For example :-  a Complex + a Complex must be defined as a friend instead of a member if you wish to allow a Float + a Complex as well (member functions don't let promotion of the left hand argument, as that would modify the class of the object it is the recipient of the member function invocation).

In other cases, select a member function on a friend function.

 

Posted Date: 3/20/2013 8:13:27 AM | Location : United States







Related Discussions:- Should my class declare a friend function or member function, Assignment Help, Ask Question on Should my class declare a friend function or member function, Get Answer, Expert's Help, Should my class declare a friend function or member function Discussions

Write discussion on Should my class declare a friend function or member function
Your posts are moderated
Related Questions

accept principal amount,rate of interest, & duration from the user. display interest amount and total amount

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

what is the purpose of pointer ? what is the syntax? how it works?

how yo use the friend function?

railway reservation programming

The concept of a string in C is difficult, because a string is a collection of characters stored in memory terminated by a NULL string i.e. \0. Let us consider the string Hello. Th

wap to print padovan string in c

A: The simplest way is to use a stringstream: #include #include #include using namespace std; string itos(int i) // convert int to string { stringstream s;

The Conditional Expression Operator An alternate method to using a simple if-else construct is the conditional expressions operator, ?:   A conditional expression operato