views:

69

answers:

4

I have the following class definition:

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

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

I created the following sub-class:

class MyQueue
    : public QueueBC<MyItemT>
{
public:

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

This compiles fine under VS2005, yet I haven't implemented constructItem() in the MyQueue class. Any idea why?

+1  A: 

It will compile but you can't create instances. MyQueue is considered abstract.

Brian R. Bondy
+5  A: 

Try using it:

MyQueue m;

You can't instantiate an abstract class, but you can define one (obviously, as you defined QueueBC). MyQueue is just as abstract.

For example:

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() {}
};
GMan
Thanks for the answer. Pretty obvious now that I think about it, my mind must not have been working very well yesterday :-(
LeopardSkinPillBoxHat
+2  A: 

Your MyQueue subclass is also abstract, just like its base class: therefore, it can't be instantiated, but just defining it (which is all you've done) is fine!

Alex Martelli
A: 

It would compile fine because the compiler does not know how you intend to use the MyQueue class. The way you have defined it, MyQueue is also a abstract class. If you try to use it only then you will get a compilation error

Yogesh Arora