定义"pure"是否有意义?基类本身中的虚函数? [英] Does it make any sense to define "pure" virtual functions in the base class itself?

查看:65
本文介绍了定义"pure"是否有意义?基类本身中的虚函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基类中定义 common 虚函数的好处是,我们不必在派生类中重新定义它们.

The benefit of defining common virtual functions in the base class is that we don't have to redefine them in the derived classes then.

即使我们在基类本身中定义了 pure 虚拟函数,我们仍然也必须在派生类中定义它们.

Even if we define pure virtual functions in the base class itself, we'll still have to define them in the derived classes too.

#include <iostream>
using namespace std;

class speciesFamily
{
    public:
        virtual void numberOfLegs () = 0;
};

void speciesFamily :: numberOfLegs ()
{
    cout << "\nFour";
}

class catFamily : public speciesFamily
{
    public:
        void numberOfLegs ()
        {
            speciesFamily :: numberOfLegs ();
        }
};

这肯定看起来很不错,但是在基类本身中定义纯虚函数有益时,是否有任何情况呢?

This may look fancy for sure, but are there any situations when it is beneficial to define a pure virtual function in the base class itself?

推荐答案

在基类本身中定义纯虚函数是否有益?

are there any situations when it is beneficial to define a pure virtual function in the base class itself?

是的-如果所讨论的函数是纯虚拟析构函数,则它必须也必须由基类定义.

Yes - if the function in question is the pure virtual destructor, it must also be defined by the base class.

这篇关于定义"pure"是否有意义?基类本身中的虚函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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