0

The old programm uses vector as a container for a self-defined type like:

std::vector<my_type> m_my_type_queue;

And it is using vector's api like:

m_my_type_queue.size();
m_my_type_queue.begin();

Now I would like to extend the queue with an extra tag, so I embeded the vector together with a tag like:

struct my_type_queue {
  std::vector<my_type> m_vector;
  mutable bool tag;
}

my_type_queue m_my_type_queue;

But the problem with this implementation is that I need to access the queue with an extra layer,like:

m_my_type_queue.m_vector.size();
m_my_type_queue.m_vector.begine();

My question is how I may access the member the same way as before but can access the tage as well, something like:

m_my_type_queue.size();      // the same as original implementation
m_my_type_queue.begin();     // the same as original implementation
m_my_type_queue.tag = true;  // new feacture with tag access

I can re-define those API for vector and forward them to the real vector but is there any easy way to do that?

thundium
  • 995
  • 4
  • 12
  • 30
  • Side note, instead of making a struct, why not use a `std::pair >`? Depending on your use, that may display the intent more clearly. – RyanP Oct 12 '16 at 15:31
  • if you're doing a lot of vector operations in one place, why not just store a local reference to the struct vector, then do all the operations on the local reference. Granted, this wont be useful if you mostly do single operations scattered throughout your code. Or if the typing really bothers you, you could add wrapper methods to the struct that wrap all the vector functions you want to use – jhbh Oct 12 '16 at 15:49

1 Answers1

1

One way to satisfy your need is to make your my_type_queue derive from std::vector.

But I would recommend a different way: using vector as is, and maintain a separate flag.

Donghui Zhang
  • 1,133
  • 6
  • 8
  • 2
    Please don't derive from `std::vector`. The standard recommends against it. – R Sahu Oct 12 '16 at 15:31
  • 2
    You can inherit `private`ly and bring at least a part of `std::vector`'s interface back, via `using` declarations rather easily. – LogicStuff Oct 12 '16 at 15:32
  • @R Sahu I agree. That's why I said I would recommend a different way. I provided that weird solution only because that's the best I could think of to directly address the original (weird) request. – Donghui Zhang Oct 12 '16 at 15:40