C++ 在没有任何纯虚方法的情况下使类抽象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4640985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a class abstract without any pure virtual methods
提问by jmasterx
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.
我有一个用来监听鼠标事件的类。但是,我不想强迫用户实现任何特定的,但我想明确表示他们必须继承它。
Is there a way to do this?
有没有办法做到这一点?
Thanks
谢谢
回答by Philip Potter
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.
}
回答by Nim
Specify the constructor of the base as protected. This does mean that you cannot construct it directly, but forces inheritance. There is nothing that makes a developer inherit from that class aside from good documentation though!
将 base 的构造函数指定为 protected。这确实意味着您不能直接构造它,而是强制继承。除了良好的文档之外,没有什么可以让开发人员从该类继承!
Example:
例子:
struct Abstract {
protected:
Abstract() {}
};
struct Valid: public Abstract {
// No need to override anything.
};
int main() {
// Abstract a; // This line fails constructor is protected
Valid v; // This compiles fine.
}
回答by Sylvain Defresne
You can declare your base class as having a pure virtual destructor that you implement. Since a destructor is always provided by the compiler, the derived class won't be purely virtual, but the base class can't directly be instantiated. You should always declare a destructor as virtualanyway, so this will have no overhead.
您可以将基类声明为具有您实现的纯虚拟析构函数。由于析构函数总是由编译器提供,派生类不会是纯虚拟的,但基类不能直接实例化。无论如何,您应该始终将析构函数声明为虚拟的,这样就不会产生开销。
class Base
{
public:
virtual ~Base() = 0;
virtual void SomeVirtualMethod();
};
inline Base::~Base()
{
}
class Derived : public Base
{
};
inline Base* createBase()
{
// return new Base; // <- This won't compile
return new Derived; // <- This does compile, Derived is not a pure virtual class !
}
回答by AbdullahC
By definition, an abstract class is a class which has at least one pure virtual function. For your purpose, you can define the destructor to be pure virtual, and provide an implementation:
根据定义,抽象类是具有至少一个纯虚函数的类。为了您的目的,您可以将析构函数定义为纯虚拟的,并提供一个实现:
class abstract {
virtual ~abstract()=0;
};
abstract::~abstract() {}
By default, all inheriting classes will not be abstract, even though they will not need to implement any method.
默认情况下,所有继承类都不会是抽象的,即使它们不需要实现任何方法。
回答by mathieu Van den Berghe
Why would you want the user to inherit, if he does not have to implement anything.
如果他不需要实现任何东西,你为什么要让用户继承。
When the base class is needed to be able to put all the "eventhandlers" in a std::set. Then no one can create a class and put it in this set without subclassing your base class. Because the set will be defined as
当需要基类能够将所有“事件处理程序”放入 std::set 时。那么没有人可以创建一个类并将其放入这个集合中而不需要继承您的基类。因为集合将被定义为
std::set<MyBaseClass*> mySet;