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
In general, Roman numerals can be converted mathematically by simply assigning a numerical value to each letter, according to the chart below, and calculating a total: M=1000 | D=5

Create a mathematical number guessing game. Have the user prompt for the number of games that they want to play. Then each game consists of the following rules. a. The computer

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Should my constructors employ "assignment" or "initialization lists"?

The Malt Shop restaurant charges $2.99 for burgers, $1.29 for fries, and $1.25 for sodas.  Write a program that allows a restaurant employee to enter an order (the number of bur


Explain Operator Overloading It is one type of Polymorphism, an significant feature of object-oriented programming .Polymorphism means one thing having many forms, i.e. here an


A: A using declaration makes it possible to employ a name from a namespace without the scope operator.

Introduction:  This assignment requires a knowledge of variables (integers, char types), loops, conditionals, switch, functions, char arrays, string arrays, number arrays, struc