使一个类的抽象没有任何纯虚拟方法 [英] Making a class abstract without any pure virtual methods

查看:160
本文介绍了使一个类的抽象没有任何纯虚拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,是要监听鼠标事件。但是,我不想强​​迫用户实现任何特定的,但我想说明他们必须继承它。

I have a class which is to listen to mouse events. However, I do not want to force the user to implement any specific one, but I do want to make it clear that they must inherit it.

有办法这样做?

感谢

推荐答案

析构函数,但给它一个定义。该类将是抽象的,但是任何继承类都不会是抽象的。

You can declare a pure virtual destructor, but give it a definition. The class will be abstract, but any inheriting classes will not by default be abstract.

struct Abstract
{
     virtual ~Abstract() = 0;
};

Abstract::~Abstract() {}

struct Valid: public Abstract
{
        // Notice you don't need to actually overide the base
        // classes pure virtual method as it has a default
};


int main()
{
    // Abstract        a;  // This line fails to compile as Abstract is abstract
    Valid           v;  // This compiles fine.
}

这篇关于使一个类的抽象没有任何纯虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆