Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class of std::vector vs non-member functions (along with a typedef) [closed]

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?

like image 543
Zachi Shtain Avatar asked Dec 13 '25 17:12

Zachi Shtain


1 Answers

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>; 
like image 107
Rob K Avatar answered Dec 16 '25 08:12

Rob K