2012-02-09 CPlusPlus0X

Explicit virtual overrides

There were some, small!?, problems regarding controlling derivation and defining virtual functions. Two new constructs override and final are introduced to resolve that.

override

The problem here was that you could by mistake create a new function when you really just wanted to override a function. By appending override to the function declaration we say that this function must override a function with the very same signature. Like this:

class Derived : public Base {
public:
   virtual void func(int) override;
};

final

With final derivation can be hindered. This can be applied both to whole classes and explicit functions.

class CantInheritFromThis final {
};

class Base {
   virtual void cantOverride(int) final;
};

Note that final and override are not keywords. They are treated as identifiers with a special meaning in the places just described. In other places they can be used just like normal identifiers.