Subclass of std::vector<T> | Sololearn: Learn to code for FREE!
0

Subclass of std::vector<T>

Hi, I want to add some functions to the std::vector<T> class, like this: template<class T> class newname : public std::vector<T> { /* ... */ };, but it doesn't work. What should I do? Hope I will get some answers, S.A.

29th Mar 2021, 5:40 PM
Sebastian Ahlborn
Sebastian Ahlborn - avatar
1 Réponse
+ 2
It is generally discouraged to inherit from std::vector<> due to the absence of a virtual destructor. If those functions don't need a vector specifically, but a container, it might make more sense to implement them as freestanding functions operating on iterators, like most algorithms in the STL. Nonetheless, it should be possible to inherit from std::vector<> without a direct problem. Unfortunately, just that snippet makes it impossible to deduce where the issue might be. Can you link your code in question here as a playground project, or at least be more specific about what doesn't work (error messages, etc.)?
29th Mar 2021, 10:45 PM
Shadow
Shadow - avatar