有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法? [英] Is there a way to flag (at compile time) "overridden" methods whose signatures don't match base signature?

查看:171
本文介绍了有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望在我的C ++代码中使用其override关键字的C#编译器功能.

Basically, I want the C# compiler functionality of its override keyword in my C++ code.

class Base
{
   virtual int foo(int) const;
};

class Derived : public Base
{
   virtual int foo(int); // wanted to override Base, but forgot to declare it const
};

众所周知,以上代码可以很好地编译,但是会产生一些奇怪的运行时行为.我希望我的C ++编译器可以通过C#的override关键字来捕获糟糕的实现.是否在C ++中引入了诸如"override"之类的关键字,还是我们坚持使用#define override virtual来表明我们的意图? (实际上,我不这样做-我讨厌使用预处理器来扩展"语言).

As we all know, the above code will compile fine, but yield some strange runtime behavior. I would love my C++ compiler to catch my poor implementation with something like C#'s override keyword. Are there any keywords like "override" being introduced into C++, or are we stuck with #define override virtual to show our intent? (actually, I do not do this - I hate using the preprocessor to "extend" the language).

推荐答案

如果您不能等待C ++ 0x,则Visual C ++已经具有此override关键字. (我相信自2005年以来).语法是:

If you can't wait for C++0x, Visual C++ already has this override keyword. (Since 2005 I believe). There the syntax is:

virtual int foo(int) override;

但是,您不必键入它.并且它是非标准的Microsoft扩展.

You're not obliged to type it, however. And its a non-standard microsoft extension.

这篇关于有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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