I am interested to know what the reason is for there to be no non-member virtual functions in C++. Especially considering the fact that it simply increases code layers when you want to achieve it, since you can define a virtual member-function and then call it from a non-member function.
EDIT: Just for reference, you can do that:
struct Base
{
virtual void say() const
{
std::cout << "Base\n";
}
};
struct Derived : public Base
{
void say() const final
{
std::cout << "Derived\n";
}
};
void say(Base* obj)
{
obj->say();
}
say(static_cast<Base*>(new Derived()));
Edit 2: And there are indeed cases where you want virtual polymorphism, since you can have the case below which doesn't work in a similar fashion, since it prints Base whereas if you were to call it with the above code, in a similar fashion it will print Derived. I believe this summarizes the crux of the problem.
void say(Base* obj)
{
std::cout << "Base\n";
}
void say(Derived* obj)
{
std::cout << "Derived\n";
}
say(static_cast<Base*>(new Derived()));