0

I have the below program where Derived class is inherited from Base class.

class Base
{   
    int p_var;
public:
    virtual void function()
    {
        cout << "Function() of class Base"<<endl;
    }
};
class Derived: public Base
{
    public:
    void function()
    {
        cout << "Function() of class Derived"<<endl;
    }
};
class Another
{
    int a_var;
    friend void function();
};
void function(Base& base, Another& anot)
{
    anot.a_var=base.p_var;
}
int main()
{
    Base *bptr,bobj;
    Derived dobj;
    bptr=&bobj;
    bptr->function();
    bptr=&dobj;
    bptr->function();
    cin.get();
    return 0;
}

In the above example I want to make "function()" a virtual member function of a "Base" class a friend to "Another" class. how should the syntax be?

arjun jawalkar
  • 138
  • 1
  • 10
  • possible duplicate of [Virtual friend functions for a base class?](http://stackoverflow.com/questions/12142893/virtual-friend-functions-for-a-base-class) – Leo Chapiro Sep 21 '14 at 10:49
  • Your code doesn't make much sense - what does the global `function` do, and how is it related to `Base::Function`? As the "possible duplicate" link says, it doesn't really make any sense to use `friend` with `virtual` functions... – Mats Petersson Sep 21 '14 at 10:54

0 Answers0