有一种方法来标记(在编译时)“覆盖”签名不匹配基本签名的方法? [英] Is there a way to flag (at compile time) "overriden" methods whose signatures don't match base signature?
问题描述
基本上,我想要在我的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#的覆盖
关键字。是否有任何关键字像重写被引入到C + +,或者我们被卡住 #define override virtual
来显示我们的意图? (实际上,我不这样做 - 我讨厌使用preprossor来扩展语言)。
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 preprossor to "extend" the language).
推荐答案
等待C ++ 0x,Visual C ++已经有这个覆盖关键字。 (自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;
但是,您没有义务输入。它是一个非标准的微软扩展。
You're not obliged to type it, however. And its a non-standard microsoft extension.
这篇关于有一种方法来标记(在编译时)“覆盖”签名不匹配基本签名的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!