Should a user-defined container that is a wrapper for std::vector, inherit or contain std::vector?
I have a class that is supposed to be a container. I see two options:
1) inherit from vector 2) have a private member vector and override all the vector functions to make my container act as vector
I am not sure if it is only a question of style, or one way is fundamentally better than the other?
The extra functionality I want to add is small, few data members and functions here and there. Mostly it will be convenient functions to work with the data in the vector.
First of all, STL containers are not supposed to be inherited. They even don't have virtual destructors.
Second, it's always preferable to choose composition/aggregation in favor of inheritance, as this is a lower coupling technique that puts less restrictions/requirements on the code.
See this answer for more details, this question has been raised a lot of times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With