0

I have a code where vector is defined like this

typedef std::vector<double> my_vec

and a matrix as

typedef std::vector<my_vec> matrix

Would it be possible to replace this definitions with a custom class which preserves all the features of the STL vector (does not break existing code) but can also be extended with new members.

In fact what I'm looking for is to simply add a bool flag to every vector.

ad1v7
  • 187
  • 1
  • 12
  • 1
    Yes, you should be able to inherit from `std::vector`. Relevant question: [Thou shalt not inherit from std::vector](https://stackoverflow.com/q/4353203/580083). – Daniel Langr Jun 15 '20 at 11:09
  • Refactor tools :). You might also consider transforming the old type into your new one with added `bool`, especially if none of the old code cares about the extra member. – George Jun 15 '20 at 11:10
  • Thanks for tips. I just gave it a try and it... almost works. Looks like I can't push back vector to matrix. I can post custom vector class code if you need it. – ad1v7 Jun 15 '20 at 11:19
  • Ignore my previous comment. It all works fine. The problem was elsewhere. Thanks for tips. – ad1v7 Jun 15 '20 at 11:54

1 Answers1

0

Following the tips I received, here is the custom class I have created.

 template <typename T>                                                                                                                                                                                    
 class fp_vec : public std::vector<T> {                                                                                                                                                                   
     public:                                                                                                                                                                                              
         using std::vector<T>::vector;                                                                                                                                                                    
         bool n = false;                                                                                                                                                                         
 };  

and the typedefs

typedef fp_vec<double> my_vec
typedef fp_vec<my_vec> matrix

I understand this is not ideal as std::vector does not have a virtual destructor so dynamic allocation is serious no no here.

ad1v7
  • 187
  • 1
  • 12