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
Consider text comprised of sentences and sentences comprised of words. Words in a sentence will be space delimited. Given a text and K strings, task is to find out the number valid

wap to calculatethe volume of cone,cylinderand sphere

write c++ program to decode the given code. in mobile keypad the integers from 1 to 9 will display the characters from a to z and 0 will assign a space

You are given a collection of words, say as in a dictionary. You can represent it in the following compressed form: the first word will be followed by a sequence of a pair of numbe

Q: What are User Defined data types? C supports an extraordinary feature known as "type definition" that permits users to define an identifier that would represent an existing

Write a C program to compute the factorial of  5 i.e 1x2x3x4x5 etc #include stdio.h   void main()   {   char promt;      int factor;   factor = 1*2*3*4*5;   printf("The fac

Inheritance and Reuse Consider a car manufacturing company. When they require building a new car, they have two choices. They can start from the scratch, or they can change an

What if one can''t wrap the local in an artificial block?

find the occurence of zy in the final string

C program to remove vowels in sentence : void main() {                 char  str[100];                 int n;                 printf("enter string\n");