Virtual Friend Function

Introduction

Background

Friend functions are not in-herited in C++. So, to achieve dynamic binding with friend functions, we have to make the interfaces as virtual (make it as protected part of class) to operate within the friend function. This is what we call as Virtual-Friend Function.

Here, the function "operator <<" in main (Base& b) will invoke b.print(o), which is virtual. So here in main that Derived-print(o) will get control, as "b" is actually a object of class Derived.

Note here that "Derived" class overrides the behavior of the protected: virtual member function print(o) and it does not have its own implmentation for friend function "operator << (ostream& o, const Base& b)."