I have the following design dilemma in my project:
I have objects of a class I've designed stored within a std::vector.
I would like to add some methods for this kind of vector (std::vector<MyClass>).
I'm thinking of encapsulating this vector in another class and add these methods which I need and of course provide all the functionality of std::vector which I use. Is this a wise idea? or should I just use non-member functions and maybe a typedef for my own convenient?
Non-member functions are the right thing in this instance. See Scott Meyer's article on the topic How Non-Member Functions Improve Encapsulation.
Also, please do use typedef std::vector<MyClass> someTypeName; You don't want std::vector<MyClass> littered throughout your code. You want what the type is not how it's implemented. If you ever have to change the implementation to use a different container, you will be quite glad you used a typedef.
ETA: in comments I am reminded of using and its superiority to typedef.
using someTypeName = std::vector<MyClass>;
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