有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?

编程入门 行业动态 更新时间:2024-10-13 20:13:51
本文介绍了有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本上,我希望在我的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.

更多推荐

有没有办法在编译时标记“覆盖"?签名与基本签名不匹配的方法?

本文发布于:2023-11-15 14:21:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   标记   不匹配   方法   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!