What is some reward/drawback of using friend functions?, C/C++ Programming

A: They present a degree of freedom in the interface design options.

Member functions & friend functions are equally privileged (100% vested). The major difference is that a friend function is called such as f(x), whereas a member function is called like x.f(). Therefore the ability to select between member functions (x.f()) and friend functions (f(x)) let a designer to choose the syntax that is deemed most readable, which lowers maintenance costs.

The major disadvantage of friend functions is that they need an extra line of code while you wished dynamic binding. In order to get the effect of a virtual friend, the friend function must call a hidden (usually protected) virtual member function. It is called the Virtual Friend Function Idiom. For instance:

class Base {

public:

friend void f(Base& b);

... protected:

virtual void do_f();

...

};

inline void f(Base& b)

{

b.do_f();

}

class Derived : public Base {

public:

... protected:

virtual void do_f(); // "Override" the behavior of f(Base& b)

...

};

void userCode(Base& b)

{

f(b);

}

In userCode(Base&) , the statement f(b) will invoke b.do_f(), that is virtual. It means that Derived::do_f() will get control actually if b is a object of class Derived. Note down that Derived overrides behavior of the protected virtual member function do_f(); this does not have its own variation of the friend function, f(Base&).

Posted Date: 3/20/2013 4:05:49 AM | Location : United States







Related Discussions:- What is some reward/drawback of using friend functions?, Assignment Help, Ask Question on What is some reward/drawback of using friend functions?, Get Answer, Expert's Help, What is some reward/drawback of using friend functions? Discussions

Write discussion on What is some reward/drawback of using friend functions?
Your posts are moderated
Related Questions
Program is to read the contents of a file: void main()    {   ifstream fin("ascii.txt");   char ch;   while(! fin.eof())     {   fin>>ch;   cout     }

In this project, you will write a software for the Registrar's office of a university called YouOIT to maintain information about students and courses at the university. The reg

Constructors and Destructors: A Class is defined as constructor by declaring a constructor in the form of function inside the class.  In other word a function in the name of

assume that the first integer read with cin specifies the number of values remaining to be entered. that program should read only one value each time cin is executed .a typical inp

Bit-wise Operators Some applications require operations to be done on dissimilar bits of a byte separately. Bit-wise operators offer a facility to do just that. There are vario

Define the Meaning of Call by reference? In "call-by- reference", as an alternative of passing the value of a variable, the location number (or the address) of the variable is

Study the given hierarchical class diagram and additional information, and answer the questions that follow: Sale Company: string unitPrice: double

For this program you will add and test 2 new member functions to the IntSLList class posted on the website. The two member functions are: insertByPosn(int el, int pos) Assuming t

Write a Program to illustrate passing structure to function? # include struct customer { int id; char name[15]; }; void func_struct(struct customer); main() { struct c

Define a user-defined data type (struct) called Car to represent a car in the context of a car dealership database. Car must have at least the following characteristics: Make Model