避免在c ++中在立即派生类中定义一个纯虚函数? [英] Avoiding Defining a pure virtual function in c++ in immediate derived class?

查看:123
本文介绍了避免在c ++中在立即派生类中定义一个纯虚函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须在c ++中的立即派生类中强制定义一个纯虚函数吗?或者我们可以避免它,以便我们可以只在具体的类中定义它?

Do we have to compulsorily define a pure virtual function in c++ in the immediate derived class ? Or can we just avoid it so that we can define it only in concrete classes ? How is this achieved ?

推荐答案

没有人强迫你实现纯 virtual 函数,所以你不必。你的类只是抽象的。在派生类中,只需离开声明或重新声明为虚拟纯。

No one forces you to implement pure virtual functions, so you don't have to. Your class will just be abstract. In a derived class, just leave the declaration out or re-declare it as virtual pure.

struct Base
{
   virtual void foo() = 0;
};

//this is OK, X is abstract
struct X : Base
{
};

//this is also OK, redundant, and Y is abstract
struct Y : Base
{
   virtual void foo() = 0;
};

这篇关于避免在c ++中在立即派生类中定义一个纯虚函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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