Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any sense in marking a base class function as both virtual and final? [duplicate]

In various explanations of C++11's final keyword, I'm seeing examples like this.

class base
{
public:
    virtual void f() final;
};

class derived : public base
{
public:
    virtual void f();    // Illegal due to base::f() declared final.
};

Is this actually a useful use of final? Why would you declare a virtual function in a base class (implying that it will be usefully overrideable in derived classes) and then immediately mark it as final (negating that implication)? What is the utility of virtual void f() final?

I can see the value of marking derived::f() final rather than base::f(). In this case, base::f() presumably had a good design-based reason for why f() should be virtual, and derived::f() separately has a good design-based reason for why no further-derived class should override its implementation.

If you don't want the function overridden polymorphically, why not just leave off the virtual keyword? Of course, derived classes might still override the function non-polymorphically. Is the purpose of virtual void f() final in the base class therefore to make base::f() firmly non-overrideable in any way—either as a virtual or non-virtual function? If so, then it seems a bit unfortunate that we must add the virtual keyword in this case only to enable use of final. I would think that it should then be legal to mark even non-virtual functions as final.

Why use virtual void f() final for a function originating in a base class when the sense of virtual and the sense of final seem to contradict?

like image 378
OldPeculier Avatar asked May 24 '13 16:05

OldPeculier


People also ask

What is the benefit of declaring base class function as virtual function?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.

Can we define virtual function in base class?

The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

Is it mandatory to override virtual function in derived class True or false?

It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. A class may have virtual destructor but it cannot have a virtual constructor.

Is virtual keyword needed in base class?

Adding the "virtual" keyword is good practice as it improves readability , but it is not necessary. Functions declared virtual in the base class, and having the same signature in the derived classes are considered "virtual" by default.


Video Answer


1 Answers

Is there any sense in marking a base class function as both virtual and final?

Yes, at least temporarily.

I found myself in a relatively large and unfamiliar existing C++ source code base. Much of the code was written prior to C++11. I found that I wanted to ensure that all overrides of a virtual function in a base class were marked with override. The hard part is locating all of those overrides.

I marked the virtual function in the base class with final, and the compiler quickly showed me where every single override was declared. It was then very easy to decorate the overrides how I wanted, and remove the final from the virtual in the base class.

like image 176
Howard Hinnant Avatar answered Sep 24 '22 02:09

Howard Hinnant