将基类类函数标记为虚拟和最终都有什么意义吗? [英] Is there any sense in marking a base class function as both virtual and final?

查看:150
本文介绍了将基类类函数标记为虚拟和最终都有什么意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各种 解释 C ++ 11的 final 关键字,我看到这样的例子。

  class base 
{
public:
virtual void f
};

类派生:public base
{
public:
virtual void f(); //非法,由于base :: f()声明final。
};

这实际上是 final ?为什么要在基类中声明一个虚函数(意味着它在派生类中可以重写),然后立即将其标记为 final (否定含义)? virtual void f()final



的效用是什么?我可以看到标记的价值 derived :: f() final而不是 base :: f()。在这种情况下, base :: f()可能有一个良好的设计原因,为什么 f()是虚拟的,而 derived :: f()单独有一个良好的设计为什么没有进一步派生类应该重写其实现的原因。



如果你不想让函数覆盖多态,为什么不离开虚拟关键字?当然,派生类可能仍然非多态地覆盖函数。因此,在基类中 virtual void f()final 的目的是使 base :: f()不可重写以任何方式 - 作为虚拟或非虚拟函数?如果是这样,那么看起来有点不幸,我们必须在这种情况下添加 virtual 关键字才能使用 final



为什么要使用 virtual void f()final

code>用于源自基类的函数,当 virtual 的感觉和 final 的感觉是否矛盾?

解决方案


将基类类函数标记为虚拟和最终?


是,至少暂时。



相对较大和不熟悉的现有C ++源代码库。许多代码是在C ++ 11之前编写的。我发现我想确保基类中的虚函数的所有覆盖都标记为 override



我在基类中用 final 标记了虚函数,并且编译器快速显示我在哪里声明每个单独的覆盖。然后很容易装饰重写我想要的,并从基类中删除最终虚拟。


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?

解决方案

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.

这篇关于将基类类函数标记为虚拟和最终都有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆