基本上,我希望在我的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.
更多推荐
有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?
发布评论