C ++样式:将虚拟关键字前缀到重写的方法 [英] C++ Style: Prefixing virtual keyword to overridden methods

查看:172
本文介绍了C ++样式:将虚拟关键字前缀到重写的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与我的同事讨论是否使用虚拟关键字或仅在原始基本类别前缀重写方法。



我倾向于以使用virtual关键字为所有虚拟方法(即涉及vtable查找的方法)添加前缀。我的理由有三个:


  1. 由于C ++缺少覆盖
    关键字,虚拟
    关键字至少通知你
    该方法涉及查找和
    理论上可以被
    进一步专门化覆盖,或者可以是
    通过指向更高的
    基类。


  2. 始终使用这种风格
    意味着,当你看到一个方法
    (至少在我们的代码中)
    virtual关键字,你可以
    最初假设它既不是从基数派生的
    ,也不是专门的子类中的


  3. 如果通过一些错误,
    virtual从IFoo中删除,所有
    孩子仍然会使用
    (CFooSpecialization :: DoBar将
    仍然覆盖CFooBase :: DoBar,
    ,而不是简单地隐藏它)。


,是但是那个方法不是虚拟的(我认为是无效的,并且是对虚拟性的误解),当我看到虚拟关键字时,我预计这意味着有人从中得到,并去寻找为他们。



假设的类可以分布在多个文件中,有几个专业。

  class IFoo {
public:
virtual void DoBar()= 0;
void DoBaz();
};

class CFooBase:public IFoo {
public:
virtual void DoBar(); //默认实现
void DoZap();
};


class CFooSpecialization:public CFooBase {
public:
virtual void DoBar(); // Specialized implementation
};在文体上,你会从两个派生类中删除virtual关键字吗?

如果是,为什么? Stack Overflow的想法是什么?

解决方案

我完全同意你的理由。这是一个很好的提醒,方法将有调用时的动态调度语义。你的同事使用的那个方法不是虚拟的论证是完全假的。他混淆了虚拟和纯虚拟的概念。


I've been having a discussion with my coworkers as to whether to prefix overridden methods with the virtual keyword, or only at the originating base class.

I tend to prefix all virtual methods (that is, methods involving a vtable lookup) with the virtual keyword. My rationale is threefold:

  1. Given that C++ lacks an override keyword, the presence of the virtual keyword at least notifies you that the method involves a lookup and could theoretically be overridden by further specializations, or could be called through a pointer to a higher base class.

  2. Consistently using this style means that, when you see a method (at least within our code) without the virtual keyword, you can initially assume that it is neither derived from a base nor specialized in subclass.

  3. If, through some error, the virtual were removed from IFoo, all children will still function (CFooSpecialization::DoBar would still override CFooBase::DoBar, rather than simply hiding it).

The argument against the practice, as I understood it, was, "But that method isn't virtual" (which I believe is invalid, and borne from a misunderstanding of virtuality), and "When I see the virtual keyword, I expect that means someone is deriving from it, and go searching for them."

The hypothetical classes may be spread across several files, and there are several specializations.

class IFoo {
public:
    virtual void DoBar() = 0;
    void DoBaz();
};

class CFooBase : public IFoo {
public:
    virtual void DoBar(); // Default implementation
    void DoZap();
};


class CFooSpecialization : public CFooBase {
public:
    virtual void DoBar(); // Specialized implementation
};

Stylistically, would you remove the virtual keyword from the two derived classes? If so, why? What are Stack Overflow's thoughts here?

解决方案

I completely agree with your rationale. It's a good reminder that the method will have dynamic dispatch semantics when called. The "that method isn't virtual" argument that you co-worker is using is completely bogus. He's mixed up the concepts of virtual and pure-virtual.

这篇关于C ++样式:将虚拟关键字前缀到重写的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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