After I write such as this:

class A {
    public: virtual void foo() = 0;
}

class B {
    public: void foo() {}
}

...B::foo() becomes virtual too. What's the rationale behind this? I'd expect it some thing such as the final keyword in Java.

Add: I understand that actually works such as this and just how a vtable works :) Now you ask ,, why C++ standard committee didn't leave a dent to call B::foo() directly and steer clear of a vtable research.

The conventional does leave a dent to call B::foo directly and steer clear of a table research:

#include <iostream>

class A {
    public: virtual void foo() = 0;
};

class B : public A {
    public: void foo() {
        std::cout <<"B::foo\n";
    }
};

class C : public B {
    public: void foo() {
        std::cout <<"C::foo\n";
    }
};

int main() {
    C c;
    A *ap = &c;
    // virtual call to foo
    ap->foo();
    // virtual call to foo
    static_cast<B*>(ap)->foo();
    // non-virtual call to B::foo
    static_cast<B*>(ap)->B::foo();
}

Output:

C::foo
C::foo
B::foo

To get the behavior you say you anticipate the following:

class A {
    virtual void foo() = 0;
    // makes a virtual call to foo
    public: void bar() { foo(); }
};

class B : public A {
    void foo() {
        std::cout <<"B::foo\n";
    }
    // makes a non-virtual call to B::foo
    public: void bar() { B::foo(); }
};

Now phone callers should use bar rather than foo. Should they have a C*, they can cast it to some-, by which situation bar will call C::foo, or they are able to cast it to B*, by which situation bar will call B::foo. C can override bar again whether it wants, otherwise not bother, by which situation calling bar() on the C* calls B::foo() as you'd expect.

I'm not sure when anybody want this behavior, though. The entire reason for virtual functions would be to call exactly the same function for any given object, regardless of what base or derived class pointer you are using. C++ therefore assumes when calls to particular member function via a base class are virtual, then calls through derived classes ought to be virtual.

Whenever you declare a virtual method, you are essentially adding a brand new entry within the vtable. Overriding a virtual method changes the need for that entry it does not take it off. This really is essentially true for languages like Java or C# too. The main difference is the fact that, with final keyword in Java, you are able to request the compiler to randomly enforce the inability to override it. C++ doesn't provide this language feature.

Simply because the course needs to possess a vtable, does not mean the compiler needs for doing things. If the kind of the item is famous statically, the compiler is free of charge to bypass the vtable being an optimisation. For instance, B::foo will most likely be known as directly in cases like this:

B b;
b.foo();

Regrettably the only method I understand to ensure this really is to check out the produced set up code.

Because technically it's virtual anything you do — it's its devote the table. The relaxation will be a syntactical police force which is where C++ differs from java.

A vtable is produced for that base class once the first virtual function is determined. Inside your example foo() comes with an entry within the vtable. Once the derived class gets in the base class it gets the vtable too. The derived class should have an entry for foo() in the vtable to ensure that the phone call is going to be rerouted properly once the derived class is recommended polymorphically via a base class pointer.