Sub-classing templated class without implementing pure virtual method
-
25-09-2019 - |
Question
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?
Solution
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() {}
};
OTHER TIPS
It will compile but you can't create instances. MyQueue is considered abstract.
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!
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