C ++:Derived + Base类实现单个接口? [英] C++: Derived + Base class implement a single interface?

查看:144
本文介绍了C ++:Derived + Base类实现单个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,是否可以有一个基类加派生类实现单个接口?

In C++, is it possible to have a base plus derived class implement a single interface?

例如:

class Interface
{
    public:
        virtual void BaseFunction() = 0;
        virtual void DerivedFunction() = 0;
};

class Base
{
    public:
        virtual void BaseFunction(){}
};

class Derived : public Base, public Interface
{
    public: 
        void DerivedFunction(){}
};

void main()
{
    Derived derived;
}

此操作失败,因为Derived无法实例化。就编译器而言,Interface :: BaseFunction从未定义过。

This fails because Derived can not be instantiated. As far as the compiler is concerned Interface::BaseFunction is never defined.

到目前为止,我发现的唯一解决方案是在Derived中声明一个传递函数。 / p>

So far the only solution I've found would be to declare a pass through function in Derived

class Derived : public Base, public Interface
{
    public: 
        void DerivedFunction(){}
        void BaseFunction(){ Base::BaseFunction(); }
};

有更好的解决方案吗?

编辑:如果重要的话,这里是一个现实世界的问题,我使用MFC对话框。

If it matters, here is a real world problem I had using MFC dialogs.

我有一个对话框类(MyDialog让我们说)从CDialog派生。由于依赖问题,我需要创建一个抽象接口(MyDialogInterface)。使用MyDialogInterface的类需要使用特定于MyDialog的方法,但也需要调用CDialog :: SetParent。我只是通过创建MyDialog :: SetParent并将它传递给CDialog :: SetParent,但是想知道是否有更好的方法。

I have a dialog class (MyDialog lets say) that derives from CDialog. Due to dependency issues, I need to create an abstract interface (MyDialogInterface). The class that uses MyDialogInterface needs to use the methods specific to MyDialog, but also needs to call CDialog::SetParent. I just solved it by creating MyDialog::SetParent and having it pass through to CDialog::SetParent, but was wondering if there was a better way.

推荐答案

C ++不会注意到继承自Base的函数已经实现了 BaseFunction :该函数必须在从 Interface 。以这种方式更改:

C++ doesn't notice the function inherited from Base already implements BaseFunction: The function has to be implemented explicitly in a class derived from Interface. Change it this way:

class Interface
{
    public:
        virtual void BaseFunction() = 0;
        virtual void DerivedFunction() = 0;
};

class Base : public Interface
{
    public:
        virtual void BaseFunction(){}
};

class Derived : public Base
{
    public: 
        virtual void DerivedFunction(){}
};

int main()
{
    Derived derived;
}

如果你只想实现其中一个, 接口分成两个接口:

If you want to be able to get away with only implementing one of them, split Interface up into two interfaces:

class DerivedInterface
{
    public:
        virtual void DerivedFunction() = 0;
};

class BaseInterface
{
    public:
        virtual void BaseFunction() = 0;
};

class Base : public BaseInterface
{
    public:
        virtual void BaseFunction(){}
};

class Derived : public DerivedInterface
{
    public: 
        virtual void DerivedFunction(){}
};  

class Both : public DerivedInterface, public Base {
    public: 
        virtual void DerivedFunction(){}
};

int main()
{
    Derived derived;
    Base base;
    Both both;
}

注意:main必须返回int

注意:好的做法是保持 virtual 在派生的成员函数前面是虚拟的基础,即使不是严格要求。

Note: main must return int
Note: it's good practise to keep virtual in front of member functions in the derived that were virtual in the base, even if it's not strictly required.

这篇关于C ++:Derived + Base类实现单个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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