Is there a way to prevent a method from being overridden in subclasses?

Viewed 20927

Is anyone aware of a language feature or technique in C++ to prevent a child class from over riding a particular method in the parent class?

class Base {
public:
    bool someGuaranteedResult() { return true; }
};

class Child : public Base {
public:
    bool someGuaranteedResult() { return false; /* Haha I broke things! */ }
};

Even though it's not virtual, this is still allowed (at least in the Metrowerks compiler I'm using), all you get is a compile time warning about hiding non-virtual inherited function X.

14 Answers

A couple of ideas:

  1. Make your function private.
  2. Do not make your function virtual. This doesn't actually prevent the function from being shadowed by another definition though.

Other than that, I'm not aware of a language feature that will lock away your function in such a way which prevents it from being overloaded and still able to be invoked through a pointer/reference to the child class.

Good luck!

(a) I dont think making function private is the solution because that will just hide the base class function from the derived class.The derived class can always define a new function with the same signature. (b) Making the function non virtual is also not a complete solution because, if the derived class redefines the same function , one can always call the derived class function by compile time binding i.e obj.someFunction() where obj is an instance of the derived class.

I dont think there is a way of doing this.Also,i would like to know the reason for your decision to prohibit derived classes from overriding base class functions.

a compile time warning about hiding non-virtual inherited function X.

change your compiler settings to make it a error instead of warning.

I guess what the compiler warns you about is hiding !! Is it actually being overridden ?

compiler might give you a warning, but at runtime, the parent class method will be called if the pointer is of type parent class, regardless of the actual type of the object it points to.

This is interesting. Try making a small standalone test program for your compiler.

If you address the child class as a type of its parent, then a non-virtual function will call the parent class's version.

ie:

Parent* obj = new Child();

Unless you make the method virtual, the child class cannot override it. If you want to keep child classes from calling it, make it private.

So by default C++ does what you want.

Trying to prevent someone from using the same name as your function in a subclass isn't much different than trying to prevent someone from using the same global function name as you have declared in a linked library.

You can only hope that users that mean to use your code, and not others', will be careful with how they reference your code and that they use the right pointer type or use a fully qualified scope.

In your example, no function is overridden. It is instead hidden (it is a kind of degenerated case of overloading). The error is in the Child class code. As csmba suggested, all you can do is changing your compiler settings (if possible) ; it should be fine as long as you don't use a third party library that hides its own functions.

Technically u can prevent virtual functions to be be overridden. But you will never ever been able to change or add more. That is not help full. Better to use comment in front of function as faq lite suggests.

C++ methods are private and un-overridable by default.

  • You cannot override a private method
  • You cannot override a non-virtual method

Are you perhaps referring to overloading?

Related