Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing overriding and/or hiding base class function (C++ 11)

I've wanted to prevent even the hiding of base class non-virtual function for a while since I learned C++, and I'm not sure if this would be ethical, but C++ 11 features gave me an idea. Suppose I have the following:

bases.h....

#ifndef baseexample_h
#define baseexample_h

#include <iostream>

class Base{
    public:  
    void foo() {
        std::cout << "Base.foo()\n" << std::endl;
    }
};

class Derived: public Base{ 
    public:
    void foo(){
        std::cout << "Derived.foo()\n" << std::endl;
    }   
};

#endif

and main.cpp...

#include "bases.h"
#include <iostream>

int main()
{

    Base base;
    Derived derived; 

    base.foo();
    derived.foo();

    std::cin.get();

    return 0;
};

for which the output is of course

Base.foo()

Derived.foo()

as the derived foo() function hides the base foo function. I want to prevent possible hiding, So my idea was to change the header file Base definition to:

//.....
class Base{
    public:  
    virtual void foo() final {
        std::cout << "Base.foo()\n" << std::endl;
    }
};

class Derived: public Base{ 
    public:
    void foo(){ //compile error
        std::cout << "Derived.foo()\n" << std::endl;
    }   
};
//......

Which seems to enforce what I want with a compiler error, prevention of overriding AND/OR hiding in c++, but my question is, is this a good practice since foo() was never a virtual function to begin with? Is there a downside to this since i'm kind of abusing the virtual keyword? Thanks.

like image 433
user27886 Avatar asked Jul 14 '13 17:07

user27886


People also ask

Which keyword in C++ 11 is used to prevent a virtual function from being overridden?

final Functions and Classes The C++11 keyword final has two purposes. It prevents inheriting from classes, and it disables the overriding of a virtual function.

How can you prevent all the member methods of base class from being overridden in C++?

In C++, there's no way to forbid it, it's just that by definition of "override", only virtual functions can be "overridden".

How can you prevent a function from being overridden?

This is the first way of preventing method overriding in the child class. If you make any method static then it becomes a class method and not an object method and hence it is not allowed to be overridden as they are resolved at compilation time and overridden methods are resolved at runtime.

How do you hide a base class function?

In Method Hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.


1 Answers

I'd say that, yes, it's bad practice. You introduced polymorphism when you didn't want it, just to prevent name hiding.

But what's so bad about name hiding? Obviously, for whatever reason, the design of Derived desires to hide the function foo in its base class; that is what it does — perhaps it makes its own call to Base::foo then performs some additional activity that is required within Derived.

Trying to subvert that is bad enough; trying to do it by co-opting language features that you don't want is even worse.

If you really need to call the base function, use derived.Base::foo(), but be aware that you are essentially breaking the API of the class Derived, which you should instead use as documented.

like image 169
Lightness Races in Orbit Avatar answered Oct 06 '22 20:10

Lightness Races in Orbit