在不实现纯虚方法的情况下对模板化类进行子类化

Leo*_*Hat 0 c++ inheritance templates pure-virtual

我有以下类定义:

template<typename QueueItemT>
class QueueBC
{
protected:
    QueueBC() {}
    virtual ~QueueBC() {}

private:
    virtual IItemBuf* constructItem(const QueueItemT& item) = 0;
} 
Run Code Online (Sandbox Code Playgroud)

我创建了以下子类:

class MyQueue
    : public QueueBC<MyItemT>
{
public:

    MyQueue() {}
    virtual ~MyQueue() {}
};
Run Code Online (Sandbox Code Playgroud)

这在VS2005下编译得很好,但我没有constructItem()MyQueue课堂上实现.知道为什么吗?

GMa*_*ckG 5

尝试使用它:

MyQueue m;
Run Code Online (Sandbox Code Playgroud)

您无法实例化抽象类,但您可以定义一个抽象类(显然,如您定义的那样QueueBC).MyQueue就像抽象一样.

例如:

struct base // abstract
{
    virtual void one() = 0;
    virtual void two() = 0;
};

struct base_again : base // just as abstract as base
{
};

struct foo : base_again // still abstract
{
    void one() {}
};

struct bar : foo // not abstract
{
    void two() {}
};
Run Code Online (Sandbox Code Playgroud)