隐式虚拟性传播的原因是什么? [英] What is the reason of implicit virtual-ness propagation?

查看:72
本文介绍了隐式虚拟性传播的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++的时间只有2到3个月,最近我发现虚函数后面的标识符 final .直到今天,我相信省略虚拟会阻止虚拟性的传播,但是我错了.它会隐式传播.

I've only been working with C++ for 2~3 months and recently I found out about the identifier, final, that comes after a virtual function. To this day, I believed that omission of virtual will stop the propagation of virtualness but I was wrong. It implicitly propagates.

我的问题是这个.为什么允许隐式传播?为什么没有 virtual 使功能虚拟化,而没有 virtual 使功能虚拟化呢?在某些情况下更好吗?还是回到第一次引入虚拟的那一天?

My question is this. Why allow implicit propagation? Why can't an existence of virtual make a function virtual and absense of virtual make a function not virtual? Is is better in some circumstance? or Was it, back in the day when virtual was first introduced?

根据 Clifford's答案,甚至有一个编译器会在缺少虚拟时生成警告.

According to Clifford's answer, there even is a compiler that generates warning upon absense of virtual.

为什么隐式传播的方法的虚拟性c

我希望上面的链接可以回答我的问题,但是没有.

I expected above link to answer my question but it doesn't.

------------添加-------------

------------ Addition -------------

有一些关于询问此功能有用性的评论. 我认为虚拟功能上的 final 关键字是使功能虚拟化的原因.该函数不能再被覆盖,因此派生类必须重新声明一个函数是否具有相同的名称. 如果最终与去虚拟化不同,请帮助我理解它. 如果最终没什么不同,那么从引入 final 的事实就可以看出去虚拟化的有用性. 我同意强迫显式虚拟会产生错误,但是我很好奇是否还有其他原因.

There are comments about asking the usefulness of this feature. I think final keyword on virtual function is what devirtualizes the function. The function can no longer by overridden, so a derived class must re-declare a function whether it has a same name or not. If final is different from devirtualization, Help me to understand it. If final is not different, then usefulness of devirtualization is self-evident from the fact final was introduced. I agree that forcing explicit virtual will produce bugs, but I'm curious if there are other reasons.

推荐答案

关于为什么存在(或不存在)特定功能的答案通常很困难,因为这成为一个问题.猜测和意见.但是,简单的答案可能是最小惊讶原则.提出一个合理且可靠且可预测的方案很困难.

The answer as to why a particular feature exists (or doesn't) is usually rather difficult, as it becomes a matter of guessing and opinions. However, the simple answer could be the principle of least astonishment. Coming up with a scheme that makes sense and works reliably and predictably would be difficult.

将功能虚拟化"甚至意味着什么?如果在运行时正在对象上调用去虚拟化"函数,它将使用指针的静态类型代替吗?如果静态类型具有虚函数,而运行时类型则没有,那么会发生什么?

What would "devirtualizing" a function even mean? If, at runtime, you're calling a "devirtualized" function on an object, would it use the static type of the pointer instead? If the static type has a virtual function but the runtime type doesn't, what happens?

#include <iostream>

struct A     {  virtual void f() const { std::cout << "A"; }  };
struct B : A {          void f() const { std::cout << "B"; }  };
struct C : B {  virtual void f() const { std::cout << "C"; }  };
struct D : C {          void f() const { std::cout << "D"; }  };

void f(const A& o) { o.f(); }

int main()
{
                // "devirtualized"     real C++

    f(A{});     // "A"                 "A"
    f(B{});     // "A" or "B"?         "B"
    f(C{});     // "C"?                "C"
    f(D{});     // oh god              "D"
}

还有一个事实,就是对于大多数设计,虚拟函数必须在整个层次结构中保持虚拟.要求全部使用virtual会引入各种难以诊断的错误. C ++通常试图避开那些需要纪律才能正确使用的功能.

There's also the fact that for the vast majority of designs, a virtual function has to stay virtual in the whole hierarchy. Requiring virtual on all of them would introduce all sorts of bugs that would be very hard to diagnose. C++ usually tries to stay away from features that require discipline to get right.

这篇关于隐式虚拟性传播的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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