What is the reason non-member virtual functions are not supported in C++

Related searches

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()));

virtual function specifier, Thread support library (C++11) default member initializer(C++11) The virtual specifier specifies that a non-static member function is virtual and supports Then this function in the class Derived is also virtual (whether or not the Attempting a virtual call through that branch causes // undefined behavior� If you find that most of your member functions are not intended to be virtual, then don't mark them virtual unless there's a reason to do so. It's a tricky issue when designing a public API, because flipping a method from one to the other is a breaking change, so you have to get it right first time.

A non-member function does not require an implicit this pointer in order to invoke it.

But virtual functions require a this pointer (i.e. an object instance) in order for polymorphism to work.

And there's the contradiction: so it's not possible to have a polymorphic non-member function.

Inheritance — <code>virtual</code> functions, C++ FAQ, What's the difference between how virtual and non- virtual member functions are called? Pseudo-code (not C++, not C) for a static table defined within file Base. cpp The idea is to cause each object's v-pointer to point at its class's v-table, as if it An idiom that allows you to do something that C++ doesn't directly support. In this program, pure virtual function virtual float area() = 0; is defined inside the Shape class. One important thing to note is that, you should override the pure virtual function of the base class in the derived class. If you fail the override it, the derived class will become an abstract class as well.

When you want to use polymorphism in free functions you basically have two options. Either you overload the function or you call virtual functions:

#include <iostream>

struct base {
    virtual void func() = 0;
};

struct foo : base { void func() { std::cout << "foo\n"; } };
struct bar : base { void func() { std::cout << "bar\n"; } };

void f(foo& f) { f.func(); }
void f(bar& f) { f.func(); }

void g(base& b) { b.func(); }

int main() {
    foo a;
    bar b;
    f(a);
    f(b);
    g(a);
    g(b);
}

Considering that the main difference to member functions is the implicit this parameter, g is actually rather close to what I'd call a "virtual free function". However, other than that there are no virtual non-member functions in C++.

Inheritance — What your mother never told you, C++ FAQ, Is it okay for a non- virtual function of the base class to call a virtual function? The most critical question in this situation is whether or not the public member function There are at least two good reasons to use protected virtuals (see below), but just Some experts, e.g., Herb Sutter's C/C++ Users Journal article Virtuality,� Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism. The prototype of virtual functions should be same in base as well as derived class. They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the

Virtual function, In object-oriented programming, in languages such as C++, and Object Pascal, a virtual If it is not 'virtual', the method is resolved 'early' and the function called is The non virtual function Move is inherited but not overridden. void Eat(void) using such purely abstract classes as interfaces works because C++ supports� Why are member functions not virtual by default? Because many classes are not designed to be used as base classes. For example, see class complex. Also, objects of a class with a virtual function require space needed by the virtual function call mechanism - typically one word per object.

12.6 — Pure virtual functions, abstract base classes, and interface , int doSomething() = 0; // Compile error: can not set non-virtual functions to 0 cannot instantiate abstract class due to following members: C:Test.cpp(128) : see declaration of Note that they can do this anyway if there's a specific reason to do so, so forcing the I presume your compiler simply doesn't support that feature. Virtual Function. A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.

a. Windows Server 2008 does not support the virtual SCSI controller used by generation one VMs in Hyper-V. b. A SCSI based drive can't be added to a generation one virtual machine. c. Generation one virtual machines do not support a non-IDE based boot drive. d. The BIOS of a generation one virtual machine will not recognize the SCSI controller.

Comments
  • The keyword virtual is tightly related to inheritance, objects and polymorphism. Non-member function have neither of those attributes.
  • what should be the effect of declaring a non-member function virtual ?
  • @user463035818 something like function overloading I guess, where you define it for some of the derived classes, and then it works even if you pass it as a base class.
  • @user463035818 A use case might be multi-dimensional polymorphic dispatching, where you have something like "virtual" functions, e.g., for a combination of 2 polymorphic pointers. C++ doesn't support it (IIRC, some languages do).
  • Perhaps it would help if you showed us what you have, what you're trying to do, ask about your actual problem instead of a vague solution (that doesn't exist)?
  • I believe that this is the closest to what I had in mind for answer - it's actually discussing the details as to why this is not implemented. Thanks a lot, if there's no better answer in a few days, I will accept this as the answer.
  • JSYK, anyone can write proposals and submit them to the C++ standards committee. If you show up at one of the meetings, you can even vote for or against proposals in plenary votes (even if you're not part of the committee). There's also a google group to suggest new ideas to the C++ standards committee (although you should have a lot of research ready to support your position)
  • Proposals are really, seriously considered. C++ has grown a lot since the old days, and it's because of community contributions. C++11 was a huge release (it's what introduced move semantics, variadic templates, lambdas, and a ton of other stuff). There was another release in 2014 and again in 2017, and the C++20 release is absolutely massive with tons of new features. C++20 is in it's final stages right now, but the next release after C++20 is gonna be in 2023, and there's plenty of time to submit a proposal for that one
  • I really encourage community involvement, and it's one of the things I like so much about C++. Here's a link that outlines how to submit a proposal: isocpp.org/std/submit-a-proposal
  • That would only work for classes where the compiler had access to the source of the class (so it wouldn't work for statically or dynamically linked libraries). With the module system coming out in C++20, that won't be as much of a problem, but it'd have to be standardized as part of the language for there to be widespread compiler support
  • How does this tie into implementation and the vtable, it's certainly feasible to implement it, isn't it?