What is the opposite of c++ `override` / `final` specifier?

萝らか妹 提交于 2019-12-05 03:13:15

The facility of specifiers like first or no_override is not there as such. Probably because it may create confusion. However, it can trivially be achieved by changing the approach.

One should add any new method in the base class with final specifier. This will help to get the compiler error for any matching signatures. Because, it will make the subsequent derived class method signatures automatically as "first" of their kind. Later the final keyword can be removed, as it was intended just for "first hand verification".

Putting & removing final keyword after the newly added base method is analogically similar to compiling binary with debug (g++ -g) option, which helps you to fix bug. In production that debug option is removed for optimization.

From your example:

class A {};  // no method, no worry

class B {
  public: virtual void showPath() = 0;  // ok
};
...

Now accidentally you are adding similar method in A, that results in error:

class A {
  public: virtual void showPath() final;  // same signature by chance
  // remove the `final` specifier once the signature is negotiated
}; 
class B {
  public: virtual void showPath() = 0;  // ERROR
};

So the signatures between new A::showPath() & existing B::showPath() have to be negotiated & then carry on by removing final specifier.

No there is not.

Adding a virtual function to a base class that has the same signature as a virtual function in a child class cannot break any existing functionality unless adding that virtual function turns the base class into a polymorphic type. So in the norm, it's benign, and a purest would argue, adding language features to guard against this would be rather pointless.

(Of course you could mark your new function final just to check that a child class function isn't going to clobber it.)

Your only option is to resort to code analysis tools.

(Note that VS2012 does not implement, or even claim to implement, the C++11 standard, although it does have some of it.)

Martin

This answer is community wiki because it combines all other answers. Please upvote the specific answer that was helpful to you as well as this one.

  1. No, there is no specifier like first or no_override. (answer)
  2. You should use the override specifier as often as possible.
    Qt has a macro Q_DECL_OVERRIDE that expands to override, if available.
    If not available, at least mark each overriding function with a comment.
  3. If you do that, there are compiler flags that warn about a missing override:
    "Clang now has -Winconsistent-missing-override, and newer GCCs have -Wsuggest-override."
    I don't know of a VS2012 flag for this. Feel free to edit.
  4. You can mimic the desired behavior by adding a 'secret' that the base class cannot know. (answer)
    This is helpful in very specific use cases, but generally breaks the concept of virtuality (see comments to the other answers).
  5. If you don't own the base class and have a conflict (e.g. compiler warning), you will need to rename your virtual function in all derived classes.
  6. If you own the base class, you can temporarily add a final to any new virtual function. (answer)
    After the code compiles without errors, you know that no function of that name and signature exists in any derived class, and you can remove the final again.

... I think I'll start marking first virtual functions as DECL_FIRST. Maybe in the future there will be a compiler-independent way of checking this.

C++ doesn't seem to provide such means out of the box. But you can mimic it like follows:

template<class Base>
class Derived : public Base
{
private:
    struct DontOverride {};

public:
    // This function will never override a function from Base
    void foo(DontOverride dummy = DontOverride())
    {
    }
};

If you intend to introduce a new virtual function, then do it like below:

template<class Base>
class Derived : public Base
{
protected:
    struct NewVirtualFunction {};

public:
    // This function will never override a function from Base
    // but can be overriden by subclasses of Derived
    virtual void foo(NewVirtualFunction dummy = NewVirtualFunction())
    {
    }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!