I have 2 classes for managing 2 queues of different types (something in the form of list<myType1*>
and list<myType1*>
).
To simplify the case, we can use here std::vector<std::string>
and std::vector<int>
as in the example below:
class StringQueue
{
public:
StringQueue();
~StringQueue();
int count() const {return m_queue.size();}
void add(std::string item) {m_queue.push_back(item);}
void clear() {m_queue.clear();}
...etc...
protected:
std::vector<std::string> m_queue;
};
class IntQueue
{
public:
IntQueue();
~IntQueue();
int count() const {return m_queue.size();}
void add(int item) {m_queue.push_back(item);}
void clear() {m_queue.clear();}
...etc...
protected:
std::vector<int> m_queue;
};
For every Queue
class, I have many functions to access the queue (add, clear, count, check, pop one, pop multiple, etc...)
Looking at the code, my question now is: is it possible to use inheritance here?
I know I can implement a Queue
base class, but only a couple of functions can be derived (count()
in the example above) since the m_queue object is different, even if the functions (add
, clear
, etc...) are the same.
Is it possible to have a complete functions inheritance even if the std::vector
elements are different?
I'm thinking something like:
class Queue
{
public:
Queue();
~Queue();
int count() const {return m_queue.size();}
void add(T item) {m_queue.push_back(item);}
void clear() {m_queue.clear();}
...etc...
protected:
std::vector<T> m_queue;
};
Is there a way to achieve this?