C ++ class method call

I am creating a new class that inherits a queue from the STL library. Vector is the only addition to the class. This vector will have the same queue size and it will store some integer values ​​that will fit every object in the queue.

Now I want to override pop () and push (), but I just want to add additional functionality to the methods of the parent class.

ex. When pop () is called on a queue object, I also want to pop the object from the vector. When push () is called on the queue object, I also want to insert a new object into the vector.

How to do it?

#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 thanks in advance

-three

+2


source to share


4 answers


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);
}

      

This will look more like:

void push(type insertMe) { 
   queue<type>::push(insertMe);
   CPU_TIME.push_back(0);
}

      



Except that you probably want to accept a parameter by const reference:

void push(type const &insertme) { 
    queue<type>::push(insertMe);
    CPU_TIME.push_back(0);
}

      

However, standard container classes are not really intended to be inherited (for example, they don't have virtual dtors), so you need to be careful with that - for example, when you destroy it, you need a static type for the derived type; you get undefined behavior if (for example) you destroy it using a pointer to the base class.

+6


source


An STL queue class that is not intended to be extended using inheritance. Take a look here for more information. It also std::queue

has more than one template argument. Instead of inheritance, you can simply use std::queue

as a member of the template class CPU_Q

like this:



template<typename T>
class CPU_Q
{
  std::queue<T> q;
public:
  void push( T val ) 
  { 
    q.push( val );
    // additional work
  }
};

      

+2


source


The simple answer is that this is not possible. STL container classes are not designed to inherit. Their destructor is not virtual. If you really want to do something like this, write a new class that "contains" a queue and a vector, and use that class everywhere. BTW as a side note, there is no keyword super

in C ++ unlike Java. If you want to call a base class method useBaseClassName::methodName();

+1


source


If your new class has no is-a relationship with std::queue

, I would strongly consider encapsulating the queue and vector, and providing methods that redirect 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 need to implement a method begin()

and end()

that returns a type iterator

that can traverse your data structure; you can use existing methods on std::queue

/ std::vector

to accomplish this.

+1


source







All Articles