Private Pure Virtual Functions in C
In C , encountering private pure virtual functions like those in the Engine class can be puzzling. As the question suggests, derived classes are expected to provide implementations for these functions, but how can they access private members of the base class to do so?
However, the question rests on a misconception: Derived classes can indeed override private pure virtual functions. They may not be able to invoke them directly, but they can provide their own implementations. This allows for "separation of the specification of the interface from the specification of the implementation's customizable behavior." (Herb Sutter, "Virtuality")
In the given example, the public interface consists of non-virtual overloaded functions that call non-public, non-overloaded virtual functions. This pattern, known as "Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals," addresses the issue of "hiding" virtual functions in derived classes.
Consider the hypothetical case where SetStateBool and SetStateInt were pure virtual functions with a non-virtual interface in the base class. Derived classes would need to protect specific overloads from being hidden by the new implementations. To do so, they would have to use the using declaration.
However, by using private virtual functions, derived classes can override these functions without worrying about hiding other methods in the base class. This simplifies the process of customizing the behavior of virtual functions in derived classes.
The above is the detailed content of How Can Derived Classes Override Private Pure Virtual Functions in C ?. For more information, please follow other related articles on the PHP Chinese website!