5

Reading that you can have final virtual functions in C++0x I am bit confused. What is the difference to just omitting both modifiers in the first place?

B_old
  • 1,141
  • 3
  • 12
  • 26

2 Answers2

10

The difference occurs it's not the base that uses it, but the derived.

class Base {
    virtual void foo() = 0;
};
class Derived : Base {
    void foo() {} 
    // Still virtual because it's virtual in base- no way to "un-virtual" it

    virtual void foo() final {} 
    // Now un-overridable.
};

Think of it not as preventing overrides, but preventing "any more" overrides.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Thanks! It was not clear to me, that the virtual gets carried through the entire hierarchy. – B_old Jul 22 '11 at 10:18
1

When I first came across the use of the final keyword in conjunction with virtual in C++, I was wondering the same thing:

If declaring a method virtual makes it inheritable and overridable, and declaring a method final prevents that method from being overriden, doesn't declaring a method both form a contradiction?

I think the current accepted answer to this question is good, but I wanted to build upon it a bit more based on what I found looking into this.

Consider the following class:

class A {
    public:
        void hide_me();
        virtual void override_me();
        virtual void cant_override_me() final;
};

The important thing to realize is that the three method declarations are all distinct and mean different things.

The first:

    void hide_me();

is non-virtual and thus, by definition, cannot be overridden.

The third:

    virtual void cant_override_me() final;

is declared final, and thus cannot be overridden, also by definition.

The difference is that since hide_me is non-virtual, overriding it is unapplicable, whereas you can think of cant_override_me as eligible to be overridden (because it is virtual,) but it also has overriding disabled due to the final modifier. In other words, overriding doesn't apply to methods that are not declared virtual, but it does apply to virtual methods, you just can't override them if they are also declared final.

Now consider a child class:

class B: public A {
    public:
        void hide_me(); // this hide's A's definition of "hide_me()"; this is not overriding.
        void override_me(); // implicit "virtual"
        //void cant_override_me(); // implicit "virtual"; compilation fails
};

You can redefine hide_me() for class B, but this is just overloading or hiding, hence the function name. B can still access A's hide_me method via A::hide_me(), but someone else who has a reference to B declared as B, i.e.:

B *my_b = new B();

must access A's now-hidden definition of hide_me via my_b->A::hide_me().

You cannot provide a redefinition of cant_override_me() in B.

As a full example, here is a slight redefinition of the program to help exemplify what's going on:

#include <cstdio>    
class A {
    public:
        inline void hide_me() {
            printf("a hide_me\n");
        }
        virtual void override_me();
        virtual void cant_override_me() final;
};

class B: public A {
    public:
        inline void hide_me() {
            printf("b hide_me\n");
        }
        void override_me();
        inline void foo() {
            A::hide_me();
        }
        // can't override cant_override_me
};

void A::override_me() {
    printf("a override_me\n");
}

void A::cant_override_me() {
    printf("a cant_override_me\n");
}

void B::override_me() {
    printf("b override_me\n");
}

int main (int argc, char *argv[]) {
    A *a = new A();
    A *ab = new B();
    B *b = new B();

    a->hide_me();
    ab->hide_me();
    b->hide_me();
    b->A::hide_me();

    printf("---\n");

    a->override_me();
    ab->override_me();
    b->override_me();
    b->A::override_me();
}

The program outputs

a hide_me
a hide_me
b hide_me
a hide_me
---
a override_me
b override_me
b override_me
a override_me
Community
  • 1
  • 1
Chris Sprague
  • 740
  • 1
  • 12
  • 22