0

Everywhere in theory, it is written that "A virtual function can be declared as a friend of another class", but practically on implementing it, the compiler throws an error saying "virtual functions cannot be friends".

Here is a C++ program to illustrate this:

#include <iostream>
using namespace std;

class extra;
class base {
public:
    virtual friend  void print(extra e);

    void show()
    {
        cout << "show base class" << endl;
    }
};

class derived : public base {
public:
    void print()
    {
        cout << "print derived class" << endl;
    }

    void show()
    {
        cout << "show derived class" << endl;
    }
};

class extra
{
    int k;
public:
    extra()
    {
        k=1;
    }
    friend void print(extra e);
};

void print(extra e)
{
    cout<<"virtual friend function "<<e.k;
}

int main()
{    
    base* bptr;
    extra e;
    derived d;
    bptr = &d;

    // virtual function, binded at runtime
    bptr->print(e);

    // Non-virtual function, binded at compile time
    bptr->show();
    print(e);
}

Output screen:

output screen

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Vanshika
  • 1
  • 6
  • What are you trying to achieve here ? Virtual functions are meant to be member functions called on an instance of a class. Having a function not in the class called "virtual" does not make sense. – Vincent Fourmond May 17 '21 at 20:25
  • You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel May 17 '21 at 20:26
  • 1
    Your reference comes from geeksforgeeks.org, a web site that's mostly a collection of meaningless programming puzzles, and they are not an authoritative source as a C++ reference. – Sam Varshavchik May 17 '21 at 20:26
  • 2
    Geeks for geeks isn't "everywhere", I think anyone can write a page, doesn't mean they know what they are talking about – Alan Birtles May 17 '21 at 20:26
  • 1
    What it probably means is that you can make a virtual function a friend of some class. You would do this by declaring them as a friend in that class, not as part of the function declaration itself. – George May 17 '21 at 20:27
  • Perhaps a duplicate: https://stackoverflow.com/questions/12142893/virtual-friend-functions-for-a-base-class – slayer May 17 '21 at 20:28
  • @AlanBirtles I agree geeksforgeeks isnt "everywhere " but this line is also in my text book and a few other sites, I provided the link just to give a reference – Vanshika May 17 '21 at 20:42

1 Answers1

8

When you write

virtual friend void print(extra e);

C++ interprets this to mean “there is a free function named print, which isn’t a member function of the current class, and it’s virtual.” That combination can’t happen, since virtual functions must be member functions of a class.

What you can do is take an existing virtual function defined in another class and make it a friend of the class. So, for example, if there’s a virtual function OtherClass::myFn, you could write

friend void OtherClass::myFn();

to say “that particular virtual function is a friend of me.” As a note, though, this just makes OtherClass::myFn a friend of the class; any overrides of OtherClass::myFn won't be friends of the class, since friendship isn't be inherited.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Worth noting that ["Friendship is neither inherited or transitive."](http://eel.is/c++draft/class.friend#10) So if the Asker is headed in the direction [I think they are trying for](https://godbolt.org/z/7YrxfPx6c) they're out of luck. – user4581301 May 17 '21 at 20:48
  • @Vanshika I think you mis-attributed. What I posted will not help because it can't compile. It was a warning. – user4581301 May 17 '21 at 21:00
  • @user4581301 since "derived" was not friend that's why It couldn't access k, removing that it worked fine. So basically virtual function (print of base) became a friend of another class (extra) (Although dynamic binding didn't come into effect as pointer->print called derived class function instead of virtual function but this code satisfies the statement) – Vanshika May 17 '21 at 21:02