Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you restrict accessibility to a virtual function in a derived class?

Consider the following code:

class Base
{
public:
    virtual void Foo() {}
};

class Derived : public Base
{
private:
    void Foo() {}
};

void func()
{
    Base* a = new Derived;
    a->Foo(); //fine, calls Derived::Foo()

    Derived* b = new Derived;
//  b->Foo(); //error
    static_cast<Base*>(b)->Foo(); //fine, calls Derived::Foo()
}

I've heard two different schools of thought on the matter:

  1. Leave accessibility the same as the base class, since users could use static_cast<Derived*> to gain access anyway.

  2. Make functions as private as possible. If users require a->Foo() but not b->Foo(), then Derived::Foo should be private. It can always be made public if and when that's required.

Is there a reason to prefer one or the other?

like image 554
Jon Avatar asked Apr 27 '12 06:04

Jon


People also ask

In which access specifier should a virtual function be defined?

7. In which access specifier should a virtual function be defined? Explanation: The virtual functions must be defined in public section of a class. This is to ensure that the virtual function is available everywhere in the program.

When should a function be virtual?

Why use virtual functions. We use virtual functions to ensure that the correct function is called for an object, regardless of the reference type used to call the function. They are basically used to achieve the runtime polymorphism and are declared in the base class by using the virtual keyword before the function.

Can a virtual function be implemented in a derived class?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Can a virtual function be declared as protected or private in the derived classes?

A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class.


1 Answers

Restricting access to a member in a subtype breaks the Liskov substitution principle (the L in SOLID). I would advice against it in general.

Update: It may "work," as in the code compiles and runs and produces the expected output, but if you are hiding a member your intention is probably making the subtype less general than the original. This is what breaks the principle. If instead your intention is to clean up the subtype interface by leaving only what's interesting to the user of the API, go ahead and do it.

like image 164
Joni Avatar answered Sep 21 '22 09:09

Joni