如何确保一个方法覆盖现有的虚拟C ++中的一个? [英] How to be sure a method is overriding an existing virtual one in C++?

查看:125
本文介绍了如何确保一个方法覆盖现有的虚拟C ++中的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们有一个基类有一个虚方法:

Let's suppose we have a base class which has a virtual method:

class BaseClass
{
    virtual void MethodToOverride() const
    {
        DoSomething();
    }
};

并且一个派生类覆盖了方法(根据情况我们可以使它再次虚拟):

And a derived class which overrides the method (depending on the situation we can make it again virtual or not):

class DerivedClass : public BaseClass
{
    void MethodToOverride() const
    {
        DoSomethingElse();
    }
}

如果我们犯了一个错误,例如定义MethodToOverride非常量或错误字符,我们只需定义一个新方法,例如:

If we make a mistake, for example defining the MethodToOverride non const or with a wrong character, we simply define a new method, for example:

void MethodToOverride() {} // I forgot the const 
void MthodToOverride() const {} // I made a typo

所以这个编译正常,但在运行时会导致不必要的行为。

So this compiles fine, but causes unwanted behavior at runtime.

有没有任何方法来定义一个函数作为显式覆盖一个现有的,所以编译器警告我if我错误地定义? (我知道它不存在):

Is there any way to define a function as an explicit override of an existing one, so the compiler warns me if I define it wrongly? Something like (I know it does not exist):

void MethodToOverride() const overrides BaseClass::MethodToOverride() const {} 


推荐答案

C ++ 0x为此提供了一个属性维特的答案),例如Visual C ++提供了一个语言扩展。

C++0x offers an attribute for this (see vitaut's answer), and e.g. Visual C++ offers a language extension.

但是在可移植的C ++ 98中,你可以做的最好的是一个健全检查,基类提供了一个可访问的成员函数相同的参数,如...

But in portable C++98 the best you can do is a sanity check, that the base class offers an accessible member function that accepts the same arguments, like ...

// The following macro is mainly comment-like, but performs such checking as it can.
#define IS_OVERRIDE_OF( memberSpec, args ) \
    suppressUnusedWarning( sizeof( (memberSpec args, 0) ) )

其中

template< typename T >
inline void suppressUnusedWarning( T const& ) {}

EDIT 添加了调用示例(免责声明:未经编译器处理):

EDIT Added call example (disclaimer: untouched by compiler's hands):

class BaseClass
{
protected:
    virtual void MethodToOverride() const
    {
        DoSomething();
    }
};

class DerivedClass : public BaseClass
{
protected:
    void MethodToOverride() const
    {
        IS_OVERRIDE_OF( BaseClass::MethodToOverride, () );
        DoSomethingElse();
    }
};

在某些情况下使用这种健全性检查可以提高代码的清晰度,在某些情况下。它有三个成本。 (1)有人Else可能会误认为担保,而不仅仅是一个信息性评论和部分检查。 (2)成员函数不能在基类中是私有的,因为它在你的例子中(虽然这可能是积极的)。 (3)有些人对任何宏的使用本能地反应(他们只是记住了关于坏的规则,而不理解它)。

Using such a sanity check can improve the clarity of the code in certain cases, and can save your ass in certain cases. It has three costs. (1) Someone Else might mistake it for a guarantee, rather than just an informative comment and partial check. (2) the member function can't be private in the base class, as it is in your example (although that's perhaps positive). (3) Some people react instinctively negatively to any use of macros (they've just memorized a rule about badness without understanding it).

hth。,

这篇关于如何确保一个方法覆盖现有的虚拟C ++中的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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