Home > Article > Backend Development > Can We Extend the C Standard Library Through Inheritance?
Extending the C Standard Library through Inheritance
The common perception is that the C standard library is not meant to be extended via inheritance. However, the existence of std::exception challenges this notion. Hence, the following questions arise:
Can Standard Library Classes Be Extended by Inheritance?
Apart from std::exception, it is not clear which other standard library classes are intended for inheritance. A practical approach to determine suitable candidates is as follows:
Implications of Inheriting from Standard Library Classes
When inheriting from a standard library class like std::exception, it is debatable whether the subclass must adhere to the original interface. However, the "Liskov Substitution Principle" (LSP) suggests that the behavior of the subclass should be consistent with that of the base class as far as the client is concerned.
Therefore, it is considered a best practice to ensure that the subclass's what() member function returns a non-null pointer, even though the Standard does not explicitly require this for subclasses of std::exception.
The above is the detailed content of Can We Extend the C Standard Library Through Inheritance?. For more information, please follow other related articles on the PHP Chinese website!