Home > Article > Backend Development > Can You Extend Other Classes in the C Standard Library Beyond `std::exception`?
Extending the C Standard Library through Inheritance
The C standard library, widely perceived as an unapproachable bastion for inheritance, belies a hidden exception within its ranks. The std::exception class stands alone as the sole entity explicitly designed for extension. This begs the question:
Can Other Standard Library Classes Be Extended?
While no definitive list exists, the answer hinges on deciphering the Standard's intent. A pragmatic approach involves scrutinizing the following criteria:
Implications of Inheritance from Standard Library Classes
Extending std::exception highlights another concern:
Adherence to Interface Expectations
When inheriting from a standard library class, the inherited interface must be respected. For instance, a subclass that alters the what() member function to return a null pointer instead of a valid description would violate the interface specified in the ISO Standard. While not strictly a standard conformance issue, it compromises correctness and undermines the principle of substitutability for base classes.
The above is the detailed content of Can You Extend Other Classes in the C Standard Library Beyond `std::exception`?. For more information, please follow other related articles on the PHP Chinese website!