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
write aprogram for random -search to implement if a[i]=x;then terminate other wise continue the search by picking new randon inex into a

Ask4. Write a query to display the columns listed below. The query should list each customer in which the video rental is overdue. The Days_Overdue column should calculate the numb

Optimize and Profile a C++ library for speed Project Description:  Optimize and Profile an existing C++ library for speed. This library uses OpenMP and OpenCV and C++ stan

A: Here are a few of the many instance of operator overloading: myString + yourString may concatenate two std::string objects myDate++ may increment a Date object a * b may m

Project Description: Currently seeking someone who can create me a software application (google chrome crx file_ that will auto add all shoe sizes to cart directly and if not av

C++ improves on many of C's features and provides object-oriented programming capabilities used for software production, quality and reusability. C++ was developed by Bjarne Strons

Aim: To implement a program to allocate memory dynamically for 2 dimensional array (accept and print matrix) using pointers. Code:                       #include #inc

Ask question #write statement that assign random integer to the varaible n in the (100

Write a simple telephone book program that stores the names and phone numbers of your friends/acquaintances in a file. Your program should have the abilities to: a. Add entries (na