Home >Backend Development >C++ >Should You Inherit from List?
The disadvantages of inheritance
List<T>
Although inherited seemingly simple, there are some problems in doing so:
In terms of performance List<T>
After a lot of optimization to improve the speed. Inheritance will destroy these optimizations, which may lead to decline in performance. Control restrictions
List<T>
is a class provided by Microsoft, you cannot directly control it. If the bottom layer of implements unexpected changes, it may lead to future compatibility issues.
Internal exposure
Inheritance from may inadvertently expose its internal implementation. This will depend on the details of changes that may occur in the future version, which will lead to potential maintenance problems.
List<T>
alternative List<T>
Use Class
List<T>
encapsulation
Collection<T>
Packaged in a custom class can provide logical separation between the list and its additional attributes. However, this will increase code redundancy and may need to forward methods for indexes and set operations. Design customized data structure Collection<T>
List<T>
Decision factors
List<T>
: Do you need to control the implementation and ensure future compatibility?
The complexity of the required function : Is the required function complicated enough to customize the data structure?
The above is the detailed content of Should You Inherit from List?. For more information, please follow other related articles on the PHP Chinese website!