2010-04-07 29 views
0

我有以下類定義:子類模板類而無需實現純虛方法

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

private: 
    virtual IItemBuf* constructItem(const QueueItemT& item) = 0; 
} 

我創建了下面的子類:

class MyQueue 
    : public QueueBC<MyItemT> 
{ 
public: 

    MyQueue() {} 
    virtual ~MyQueue() {} 
}; 

這將編譯VS2005下細,但我在MyQueue類中沒有實現constructItem()。任何想法爲什麼?

+0

如果你不能編譯這樣的代碼,那麼庫會有很多問題。 – Tom 2010-04-07 01:40:16

+0

@湯姆 - 絕對!如果我在發佈之前再想幾秒鐘,我會意識到問題所在(我沒有在代碼中實例化對象)。 – LeopardSkinPillBoxHat 2010-04-07 23:51:06

回答

5

嘗試使用它:

MyQueue m; 

不能實例化一個抽象類,但你可以定義一個(很明顯,當你定義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() {} 
}; 
+0

感謝您的回答。現在相當明顯,我想到了這一點,昨天我的思想一直不太好.--( – LeopardSkinPillBoxHat 2010-04-07 23:49:52

2

它會編譯,但你不能創建實例。 MyQueue被認爲是抽象的。

2

MyQueue子類也是抽象的,就像它的基類;因此,它不能被實例化,只是定義它(這是所有你」已經完成)很好!

1

它編譯得很好,因爲編譯器不知道你打算如何使用MyQueue類。您定義它的方式,MyQueue也是一個抽象類。如果你嘗試使用它,那麼你將得到一個編譯錯誤