Private Virtual Methods in C
In object-oriented programming, a virtual method is a member function that can be overridden by subclasses. While it's common for virtual methods to be public or protected, they can also be declared private. But what's the advantage of doing so?
In the code snippet from an open source C project, you've observed that two private methods are declared virtual: childAllowed and createElement. To shed light on this practice, let's explore the benefits of making private methods virtual.
Herb Sutter's Guidelines on Virtual Methods
According to software expert Herb Sutter, the preferred approach is to make virtual functions private. He explains that this strategy allows derived classes to override the function and customize its behavior as needed, without exposing them directly to be invoked by derived classes' code.
Benefits of Private Virtual Methods
Making a private method virtual provides several advantages:
The above is the detailed content of Why Make Private Methods Virtual in C ?. For more information, please follow other related articles on the PHP Chinese website!