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

23.3k Views Asked by At

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

There are 14 best solutions below

7
OJ. On BEST ANSWER

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!

0
Ryan Fox On

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();
1
Frank Krueger On

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?

0
csmba On

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

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

0
Mark Harrison On

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.

2
Danny Whitt On

Sounds like what you're looking for is the equivalent of the Java language final keyword that prevents a method from being overridden by a subclass.

As others here have suggested, you really can't prevent this. Also, it seems that this is a rather frequently asked question.

0
Mark Cidade On

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.

0
AudioBubble On

(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.

0
Luc Hermitte On

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.

0
RuntimeException On

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.

0
AudioBubble On

For clarification, most of you misunderstood his question. He is not asking about "overriding" a method, he is asking whether there is a way to prevent "hiding" or not. And the simple answer is that "there is none!".

Here's his example once again

Parent class defines a function:

int foo() { return 1; }

Child class, inheriting the Parent defines the same function AGAIN (not overriding):

int foo() { return 2; }

You can do this on all programming languages. There is nothing to prevent this code from compiling (except a setting on the compiler). The best you'll get is a warning that you are hiding the parent's method. If you call the child class and invoke the foo method, you'll get 2. You have practically broken the code.

This is what he is asking.

0
Vlad Sakharuk On

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.

3
moooeeeep On

When you can use the final specifier for virtual methods (introduced with C++11), you can do it. Let me quote my favorite doc site:

When used in a virtual function declaration, final specifies that the function may not be overridden by derived classes.

Adapted to your example that'd be like:

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

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

When compiled:

$ g++ test.cc -std=c++11
test.cc:8:10: error: virtual function ‘virtual bool Child::someGuaranteedResult()’
test.cc:3:18: error: overriding final function ‘virtual bool Base::someGuaranteedResult()’

When you are working with a Microsoft compiler, also have a look at the sealed keyword.

1
Abhay Bhave On

I was searching for same and yesterday came to this [rather old] question.

Today I found a neat c++11 keyword : final . I thought it may be useful for next readers.

http://en.cppreference.com/w/cpp/language/final