Reputation: 826
I'm creating a new class that inherits queue from the STL library. The only addition to the class is a vector. This vector will have the same size of the queue and it will store some integer values that will correspond to each objects in the queue.
Now, I want to override pop() and push(), but I simply want to add more functionality to the parent's class methods.
ex. When pop() is called on the queue object, I also want pop an object from the vector. When push() is called on the queue object, I also want insert a new object into the vector.
How do I do that???
#include <iostream>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
template <typename type>
class CPU_Q : public queue<type>
{
public:
vector<int> CPU_TIME;
void increaseTime()
{
for(int ndx = 0; ndx < CPU_TIME.size(); ndx++)
{
CPU_TIME[ndx]++;
}
}
void push(type insertMe)
{
//This is what I want to do
super::push(); // or queue::push(); maybe?
CPU_TIME.push_back(0);
}
void pop()
{
//Something very similar to push()
}
}
Many Many thanks in advance
-Tri
Upvotes: 2
Views: 3717
Reputation: 99585
STL queue class in not intended to be extended using inheritance. Look here for more information on that. Besides that std::queue
has more than one template argument. Instead of inheriting you could just use std::queue
as a member of your template class CPU_Q
as follows:
template<typename T>
class CPU_Q
{
std::queue<T> q;
public:
void push( T val )
{
q.push( val );
// additional work
}
};
Upvotes: 2
Reputation: 20200
Unless your new class has a is-a relationship with std::queue
, I would strongly consider encapsulating the queue and vector, and providing methods that forward to the appropriate std::queue
/std::vector
methods, in the order you want them to be called.
Also, if you want this new class to be compatible with standard algorithms, you will have to implement a begin()
and end()
method that return an iterator
type capable of walking your data structure; you may be able to use the existing methods on std::queue
/std::vector
to accomplish this though.
Upvotes: 1
Reputation: 490148
You asked about:
void push(type insertMe){
//This is what I want to do
super::push(); // or queue::push(); maybe?
CPU_TIME.push_back(0);
}
That would be more like:
void push(type insertMe) {
queue<type>::push(insertMe);
CPU_TIME.push_back(0);
}
Except you probably want to accept the parameter by const reference:
void push(type const &insertme) {
queue<type>::push(insertMe);
CPU_TIME.push_back(0);
}
That said, the standard container classes aren't really designed for inheritance (e.g. they don't have a virtual dtors), so you'll have to be careful with this -- e.g. when you destroy it, you'll need the static type to be the derived type; you'll get undefined behavior if (for example) you destroy one via a pointer to the base class.
Upvotes: 6
Reputation: 1006
Simple answer is that it is not possible. STL container classes are not designed to be inherited. Their destructor is not virtual to start. If you really want to do something like this then write a new class which 'contains' the queue and vector , and use this class everywhere. BTW as a side note, there is no super
keyword in C++ unlike Java. If you want to call base class method use BaseClassName::methodName();
Upvotes: 1