tags:

views:

1280

answers:

13

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.

A: 

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();
Ryan Fox
+9  A: 

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!

OJ
A: 

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?

Frank Krueger
We do can override private virtual functions.
Luc Hermitte
+1  A: 

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

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

csmba
A: 

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.

Mark Harrison
+4  A: 

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.

Danny Whitt
A: 

@Danny:

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.

Not really. Actually, final is the default behaviour in C++ and virtual methods have to be marked explicitly. However, unlike Java, C++ offers nonvirtual inheritance, which is where this particular problem occurs.

/EDIT: OK, this is (partly) bogus. final isn't the same as nonvirtual.

Konrad Rudolph
This should be a comment. -1
the_drow
@The_drow: What a thoroughly pointless comment, and downvote. This answer was posted long before comments were implemented, and your deputy sheriff attitude won’t endear you to other users.
Konrad Rudolph
A: 

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.

Mark Cidade
+1  A: 

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

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.

Luc Hermitte
+1  A: 

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.

Satish Motwani
A: 

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(){returns 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.

Icy
A: 

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.

Vlad Sakharuk