开发者

How do I override some functions in the Queue STL object?

I am trying to override the push and pop functions from the STL queue. I think I need to use templates. I get a 'MyQueue' does not name a type (this error is in my main.cpp file) and an error saying expected initializer before '<' token. Here is the snippet of code:

#include "MyQueue.h"

sem_t EmptySem;
sem_t PresentSem;
sem_t mutex;

MyQueue::MyQueue()
{
    sem_init(&EmptySem, PTHREAD_PROCESS_SHARED, QSIZE);
    sem_init(&PresentSem, PTHREAD_PROCESS_SHARED, 0);
    sem_init(&mutex, PTHREAD_PROCESS_SHARED, 1);
}

template <class Elem>
void queue<Elem>::push(const Elem &Item)
{
    sem_wait(EmptySem);
    sem_wait(mutex);
    super.push(Item);
    sem_post(mutex);
    sem_post(PresentSem);
} 
template <class Elem>
Elem queue<Elem>::pop(void)
{
    Elem item;
    sem_wai开发者_StackOverflowt(PresentSem);
    sem_wait(mutex);
    item = super.front();
    super.pop();
    signal(mutex);
    signal(EmptySem);
    return item;
}

Thanks!


You cannot override functions that are not defined as virtual. So you gain nothing by publicly inheriting from std::queue.

It would be best if MyQueue stored a member that was a std::queue. That way, you can do whatever you want and just forward the functions to the std::queue member.

Also, C++ has no keyword super; that's Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜