具有抽象方法和虚方法的类

发布于 2024-12-09 14:52:05 字数 127 浏览 0 评论 0原文

我可以编写一个具有虚拟方法(可以被重写但具有默认行为)和抽象方法(必须被重写)的类吗?

我可以有一个定义,在其中定义虚拟方法的实现而不是抽象方法吗?

此外,我可以创建在非抽象/纯虚拟类中没有实现的抽象方法吗?

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

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

发布评论

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

评论(3

浅黛梨妆こ 2024-12-16 14:52:05

我可以编写一个具有虚拟方法 - (可以被重写但具有默认行为)和抽象方法 - (必须被重写)的类

吗?

我可以有一个定义来定义虚拟的实现而不是抽象吗?

是的。

此外,我可以创建在非抽象/纯虚拟类中没有实现的抽象方法吗?

不可以。纯虚拟成员的存在会禁止您创建类实例,即使类变得抽象。

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)

Yes.

Can I have a definition in which I define implementations for the virtuals but not the abstracts?

Yes.

Additionally can I create abstract methods that don't have an implementation in a non abstract / pure virtual class?

No. The presence of pure virtual members prohibits you from creating class instances, i.e. makes the class abstract.

平安喜乐 2024-12-16 14:52:05

一些虚方法可以有合理的默认实现,而其他纯虚方法必须在派生类中实现。

It' s ok to have some virtual methods with reasonable default implementation, while other pure virtual methods have to be implemented in derived classes.

躲猫猫 2024-12-16 14:52:05

是的,你可以。如果您的类有纯虚拟方法,那么该类将是抽象的并且无法实例化,但您可以实现所有其余方法(虚拟或其他方法),并且继承类将能够访问它们。

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.

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