具有抽象方法和虚方法的类
我可以编写一个具有虚拟方法(可以被重写但具有默认行为)和抽象方法(必须被重写)的类吗?
我可以有一个定义,在其中定义虚拟方法的实现而不是抽象方法吗?
此外,我可以创建在非抽象/纯虚拟类中没有实现的抽象方法吗?
Can I write a class which has virtual methods - (which can be overridden but have a default behaviour) and also abstract methods - (which have to be overridden)
Can I have a definition in which I define implementations for the virtuals but not the abstracts?
Additionally can I create abstract methods that don't have an implementation in a non abstract / pure virtual class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
吗?
是的。
不可以。纯虚拟成员的存在会禁止您创建类实例,即使类变得抽象。
Yes.
Yes.
No. The presence of pure virtual members prohibits you from creating class instances, i.e. makes the class abstract.
一些虚方法可以有合理的默认实现,而其他纯虚方法必须在派生类中实现。
It' s ok to have some virtual methods with reasonable default implementation, while other pure virtual methods have to be implemented in derived classes.
是的,你可以。如果您的类有纯虚拟方法,那么该类将是抽象的并且无法实例化,但您可以实现所有其余方法(虚拟或其他方法),并且继承类将能够访问它们。
Yes you can. If there's a pure virtual method to your class then class will be abstract and can't be instantiated, but you can implement all the rest of the methods (virtual or otherwise) and the inheriting classes would be able to access them.