I'm in the proccess of learning the language and this is a noob doubt.
Is it possible to use a virtual friend function? I don't know if it's possible, I didn't even test it but it could be useful in some situations. For example, for the overloaded operator<<().
DerivedClass dc;
BaseClass &rbc = dc;
cout << rbc;
My guess is it's possible, but I'm not sure since a friend function is not implemented in the class design, and theoretically is not part of it (though in this example, conceptually it makes sense that operator<<() should be a method, but due to syntax limitations it's not possible to implement it as one).
EDIT: my concern is related with this example:
BaseClass bc;
DerivedClass dc;
BaseClass *pArr[2];
pArr[1] = bc;
pArr[2] = dc;
for (int i = 0; i < 2; i++)
cout << pArr[i];
in this array of mixed objects, I want the correct operator<<() called for each one.
See Question&Answers more detail:os