Dynamically change class attribute type

I am coding a class that has a vector as a private attribute. The type of vector (it can be concurrent_vector from the TBB library or std-vector) is known only at runtime, depending on the parameter specified by the user.

So the question is, how can I code this? I thought something like:


class A {
  private:
     void* vec;
  public:
    A( int type ) {
       if ( type == 1 ) {
         // convert the void* into std::vector<>
      } else {
         // convert into a tbb::concurrent_vector
      }
  }
};

This conversion, can this be done by reinterpret_cast? Or is there another better way to do this?

I am empty. Thank you for your time.

+3
source share
1 answer

If you have a fixed set of types, this boost::variant<>may be the appropriate solution.

boost::variant< tbb::concurrent_vector<int>, std::vector<int> > member;

, ( new, ) . , boost::any

boost::any member;

.

+6

Source: https://habr.com/ru/post/1790220/


All Articles