子类化模板类而不实现纯虚方法
我有以下类定义:
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()
。知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它将编译,但您无法创建实例。 MyQueue 被认为是抽象的。
It will compile but you can't create instances. MyQueue is considered abstract.
您的
MyQueue
子类也是抽象的,就像它的基类一样:因此,它不能实例化,而只能定义它(即你所做的一切)都很好!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!它可以很好地编译,因为编译器不知道您打算如何使用 MyQueue 类。按照您定义的方式,
MyQueue
也是一个抽象类。如果您尝试仅使用它,则会出现编译错误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尝试使用它:
您无法实例化抽象类,但您可以定义一个抽象类(显然,正如您定义的
QueueBC
)。MyQueue
也同样抽象。例如:
Try using it:
You can't instantiate an abstract class, but you can define one (obviously, as you defined
QueueBC
).MyQueue
is just as abstract.For example: