开发者

Enforcing Correct Virtual Function Definitions in Derived Classes [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Making sure the method declaration is inherited

Hello, I have difficulty from time to time enforcing the appropriate definitions of virtual member functions in my class hierarchies. If I mis-define a virtual function inheriting from an interface the bug will quickly come to light as I typically cannot instantiate the derived class. However if inherit from say a Base Class (non-abstract) which in turn has inherited from the interface the bugs become more subtle: ie. in the misdefinition (eg. VIsVisible() vs VisVisible() or small changes in parameter types incl const) then I end up defining and calling functions outside the hierarchy when I think I am inside it.开发者_StackOverflow中文版 Ideally, when I define the function inside the derived class, I want the compiler to flag it if its not consistent with an existing virtual function in the Base Class by introducing some kind of keyword.... how do others manage this issue? Thanks for your help/ideas!


You want to use the override keyword:

class Base
{
    virtual void MyFunction() { /*Blah*/ }
};

class Child : Base
{
    virtual void MyFunction() override { /* DoSomething*/ }
};

If you change the definition of Base::MyFunction you will now get a compile error.

override documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜