子类化模板类而不实现纯虚方法

发布于 2024-08-28 02:37:04 字数 476 浏览 5 评论 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() 。知道为什么吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我喜欢麦丽素 2024-09-04 02:37:05

它将编译,但您无法创建实例。 MyQueue 被认为是抽象的。

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

爱的那么颓废 2024-09-04 02:37:05

您的 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!

初见你 2024-09-04 02:37:05

它可以很好地编译,因为编译器不知道您打算如何使用 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

梦纸 2024-09-04 02:37:04

尝试使用它:

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() {}
};

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() {}
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文