Making a derived member function impossible to override?

Started by
14 comments, last by smr 15 years, 5 months ago
Hi All, Does anyone have any idea on how to make a derived classes member function impossible to override? For example:

class A
{
    void Foo(){};
};

class B : public A
{
    void Foo(){}; // We want to make this fail on compile
};

B b;
b.Foo(); // Happily calls the function from class B

Thanks :)
Advertisement
This is definitely not part of standard C++, but it does work in visual C++, so you might want to macro it up....

#ifdef _MSC_VER# define MY_SEALED sealed#else# define MY_SEALED #endifclass A{    virtual void Foo() MY_SEALED {};};class B : public A{    void Foo() {}; // fails to compile.};


Don't make it virtual. If it absolutely has to be virtual, make it private and have a public function call it.

This is one of the reasons why virtual functions should never be public, only protected: you can make a virtual function private and therefore prevent any overrides.
Hey ToohrVyk, we aren't making it virtual as you can see from the pseudo code source I gave. Thats actually part of the problem, We *never* want this overidded.

RobTheBloke, thats great :) I'm suprised it's not a standard keyword in C++ to be honest. It's a nice way to protect your classes from abuse.
Quote:Original post by badger82
Hey ToohrVyk, we aren't making it virtual as you can see from the pseudo code source I gave. Thats actually part of the problem, We *never* want this overidded.


Ah. Since you were using the term overriding I assumed that you referred to virtual functions (non-virtual functions are overloaded, not overridden).

I am not aware of any means of preventing overloading of functions from the base class. However, I am not aware of any problems (or potential abuse) related to it either.
Thanks ToohrVyk for correcting my mistake there. Essentially we have had instances where a derived class overloads the function from a base class and while this is perfectly legal in some instances it is really not desirable. If we have a way like "sealed" to tell both the compiler and anyone looking at the header that you shouldn't under any circumstances overload or override this function then it would just be handy ;)
arboreal
I think ToohrVyk was basically saying that declaring virtual funcs for the sake of it isn't a great idea, and i'm tempted to agree - which is certainly one of the problems of my original code snippet. I'd almost be tempted to do something like this, to prevent the virtuals getting defined in the release build, but still giving you the compile errors in debug. maybe.

#if defined(_MSC_VER) && defined(_DEBUG)# define CANNOT virtual# define BE_OVERRIDDEN sealed#else# define CANNOT# define BE_OVERRIDDEN #endifclass A{    CANNOT void Foo() BE_OVERRIDDEN {};}
My solution:
// This class is not intended to be derived...// This function should not be overriden


Seems to be working fine for standard and many other libraries.

Other approach:
struct Foo {  void bar() { internal_bar(); }private:  void internal_bar() {    x += 1;  }  int x;};
People can override, but cannot replicate required behavior, causing the class to break. Then again:
*((int*)(((char *)(&foo))+22)) = 1;
or something along these lines...
@Sneftel - Lol, that was an option that we had already considered. Personally I had never come across this problem before in all my time coding with C++. But I can see why someone relatively new to C++ may overload instead of override for example, we just want something to moan at them when they do ;)

@RobTheBloke - Yeah I completely agree with his comment on not declaring virtuals for the sake of it and think your solution is a nice way to go about it.



This topic is closed to new replies.

Advertisement