A non-virtual idiom interface for more than two levels of inheritance?

The non-virtual interface initiative describes how virtual methods are non-public settings points, and public methods are not virtual, to allow the base class to always control how the settings points are invoked.

This is an elegant idiom, and I like to use it, but how does it work if the derived class is itself a base class

+5
source share
2 answers

This works because a derived class can override a private virtual function of a base class, even if the function of the base class overrides the function of the base class.

This is completely legal:


class Parent
{
public:
  int foo() {return bar();} // the non-virtual public interface
private
  virtual int bar();
};

class Child : public Parent
{
private:
  virtual int bar();  // overrides Parent::bar()
};

class Grandchild : public Child
{
private:
  virtual int bar(); // overrides Child::bar();
};
+5
source

The resulting class can solve by itself:

You can completely override a method by implementing a virtual function. You can increase the method by calling the function of the "middle" classes at some point in your derived class method.

If this is not what you want, you need to explicitly set it in the "middle" class. I would not want to. If you want this, it probably means that you did not give the base class enough tuning points.

+1
source

All Articles