Home > front end >  Why private virtual member function of a derived class is accessible from a base class
Why private virtual member function of a derived class is accessible from a base class

Time:04-10

Consider the following snippet of code:

#include <iostream>
    
class Base
{
public:
  Base()
  {
      std::cout << "Base::constr" << std::endl;
      print();
  }
  virtual ~Base() = default;

  void print() const
  {
      printImpl();
  }
  
private:
    virtual void printImpl() const
    {
        std::cout << "Base::printImpl" << std::endl;
    }
};

class Derived : public Base
{
public:        
    Derived()
    {
        std::cout << "Derived::constr" << std::endl;
    }
    
private:
    void printImpl() const override
    {
        std::cout << "Derived::printImpl" << std::endl;
    }
};

int main()
{
    Base* ptr = new Derived();
    ptr->print();
    delete ptr;

    return 0;
}

The above code will print the following:

Base::constr

Base::printImpl

Derived::constr

Derived::printImpl

but I don't understand why printImpl private function is accessible from the base's print function. In my understanding this pointer implicitly passed to the print function holds the derived object's address, but I thought private member functions could be called ONLY from the member functions (and from friend functions) of the same class and here, Base class is not the same class as Derived, although there is an is a relationship.

CodePudding user response:

The private function of base class is not accessible in the derived class, but it can be overriden, since these two are separate concepts.

For the print call from the constructor of the Base class, there is a following rule:

In a constructor, the virtual call mechanism is disabled because overriding from derived classes hasn’t yet happened. Objects are constructed from the base up, “base before derived”.

P.S. In general calls to virtual functions from constructors or destructors better be avoided

CodePudding user response:

I don't understand why printImpl private function is accessible from the base's print function.

From this pionter's documentation:

When a non-static class member is used in any of the contexts where the this keyword is allowed (non-static member function bodies, member initializer lists, default member initializers), the implicit this-> is automatically added before the name, resulting in a member access expression (which, if the member is a virtual member function, results in a virtual function call).

So, when you wrote:

ptr->print();  //this is equivalent to writing (*ptr).print();

The above statement is equivalent to writing:

(*ptr).print();

This means that the address of the object to which the pointer ptr points(which is nothing but ptr itself) is implicitly passed to the implicit this parameter of the print member function.

Now, inside the print member function, the statement containing the call to printImpl() is equivalent to:

this->printImpl();

according to the quoted statement at the beginning of my answer. And from the same quoted statement, since the member printImpl is a virtual member function, the expression this->printImpl() results in a virtual function call, meaning effectively calling the derived class printImpl function.

CodePudding user response:

The reason that in the code you have Base::printImpl printed is because it is called from constructor of Base. The derived class wasn't constructed yet, so all calls to virtual functions will be referred to Base's versions.

After construction the calls to print will akways redirect towards derived class's version. Whether functions are marked private, public, protected is inconsequential here.

CodePudding user response:

First, as @Eljay notes - printImpl() is a method, albeit virtual, of the Base class. So, it's accessible from the base class. Derived merely provides a different implementation of it. And the whole point of virtual functions is that you can call a subclass' override using a base class reference or pointer.

In other words, private only regards access by subclasses; it's meaningless to keep something private from a class' base class: If a method is at all known to the base class, it must be a method of the base class... a virtual method.


Having said all that - note that the Derived version of printImpl() is effectively inaccessible from print() - when it's invoked within the base class constructor. This is because during that call, the constructed vtable is only that of Base, so printImpl points to Base::printImpl.

I thought private member functions could be called ONLY from the member functions of the same class

And indeed, print() is a member of Base, which invokes printImpl() - another method of Base.

  • Related