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
describe how identifiers of different data type are defined using this notaion?

Write a Program to illustrate Array with Strings? main() { static char name[]="devdas"; int i; i=0; while(name[i]!='\0') { printf("%c",name[i]); i=i+1; } } In the

C Program for SORTING # include stdio.h> void main() {           char a;           int *p;           int i,j,temp;           clrscr();           p=&i;

search words from a passage and return snippets in html format..

Explain high-order and low-order bytes. - Numbers are written from left to right in decreasing order of significance. In the same way, bits in a byte of computer memory can be

A: Provide a friend operator class Base { public: friend std::ostream& operator ... protected: virtual void printOn(std::ostream& o) const; }; inline std::ostr


write a program to find the area under the curve y=f(x) between x=a & x=b

Introduction to Classes Object-oriented programming (OOP) is a conceptual approach to design programs. It can be executed in many languages, whether they directly support OOP c

Damjibhai and Shamjibhai are two jeweler friends. They decide to play a simple game. The game comprises of removing the jewels for polishing, turn by turn. Once a jewel is removed