[英]C++ Templates and Polymorhpism?
我试图在C ++中创建java Queue接口。 从它继承时,我无法正确编译它。
这是QueueInterface.h
#ifndef QUEUEINTERFACE_H_
#define QUEUEINTERFACE_H_
/**
* Java like interface
*/
template<class E>
class QueueInterface {
public:
virtual bool add(E e);
virtual E element();
virtual bool offer(E e);
virtual E peek();
virtual E poll();
virtual E remove();
virtual int size();
virtual bool isEmpty();
virtual ~QueueInterface();
};
#endif /* QUEUEINTERFACE_H_ */
这是LinkedQueue.h
#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
LinkedList<T> linkedList;
public:
//DOES NOT COMPILEE
// virtual bool QueueInterface<T>::add(T t) {
// return false;
// }
// virtual T QueueInterface<T>::element() {
// T t;
// return T;
// }
// virtual bool QueueInterface::offer(T e) {
// return false;
// }
// virtual T QueueInterface::peek() {
// T t;
// return t;
// }
// virtual T QueueInterface::poll() {
// T t;
// return t;
// }
// virtual T QueueInterface::remove() {
// T t;
// return t;
// }
// virtual int QueueInterface::size() {
// return -1;
// }
// virtual bool QueueInterface::isEmpty() {
// return false;
// }
// virtual QueueInterface::~QueueInterface() {
// }
};
#endif /* QUEUE_H_ */
如何使用模板和多态性来模拟Queue接口?
编辑:我必须改变的事情
接口:// = 0添加到函数
#ifndef QUEUEINTERFACE_H_
#define QUEUEINTERFACE_H_
/**
* Java like interface
*/
template<class E>
class QueueInterface {
public:
QueueInterface() {
}
virtual bool add(E e)=0;
virtual E element()=0;
virtual bool offer(E e)=0;
virtual E peek()=0;
virtual E poll()=0;
virtual E remove()=0;
virtual int size()=0;
virtual bool isEmpty()=0;
virtual ~QueueInterface() {
}
;
};
#endif /* QUEUEINTERFACE_H_ */
LinkedQueue实现(不起作用只是试图使继承起作用):
#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
LinkedList<T> linkedList;
public:
LinkedQueue() {
}
virtual bool add(T t) {
std::cout << "entering add";
return false;
}
virtual T element() {
T t;
return t;
}
virtual bool offer(T e) {
return false;
}
virtual T peek() {
T t;
return t;
}
virtual T poll() {
T t;
return t;
}
virtual T remove() {
T t;
return t;
}
virtual int size() {
return -1;
}
virtual bool isEmpty() {
return false;
}
virtual ~LinkedQueue() {
}
};
#endif /* QUEUE_H_ */
如果要从Java过渡到c ++,请记住,与在Java中为true或false相比,在c ++中打印布尔值会产生0或1。
您应该使用LinkedList成员实现在队列接口中定义的操作:
#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
LinkedList<T> linkedList;
public:
virtual ~LinkedQueue() {
}
virtual bool add(T t) {
linkedList.push_back(t);
}
virtual T element() {
return linkedList.front();
}
virtual size() {
return linkedList.size();
}
virtual isEmpty() {
return linkedList.empty();
}
// etc...
// you may need to do some blocking operations
// for some of the members like poll
};
显然我没有LinkedList的接口,所以列表成员函数调用基于std :: list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.