gpt4 book ai didi

c++ - 如何正确扩展接口(interface)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:19 26 4
gpt4 key购买 nike

我有基于另一个的界面:

class IDrawable {
public:
virtual ~IDrawable();
};

class IExtendedDrawable: public IDrawable {
public:
virtual ~IExtendedDrawable();
};

class DrawableImplementation: public IDrawable {
public:
virtual ~DrawableImplementation();
};

class ExtendedDrawableImplementation:
public DrawableImplementation, public IExtendedDrawable
{
public:
virtual ~ExtendedDrawableImplementation();
};

然后ExtendedDrawableImplementation = DrawableImplementation(+IDrawable) + IExtendedDrawable(+IDrawable)

在同一个类中有两次IDrawable是否正确?

最佳答案

我会怀疑您确实需要/想要多重继承。我只在有限的情况下认为它很好,接口(interface)就是其中之一(甚至 Java 也允许这样做)。

如上所述,使用虚拟继承并确保在接口(interface)es中只使用纯虚拟方法。

class IDrawable {
public:
virtual ~IDrawable();
virtual void doSomething() = 0;
};

class IExtendedDrawable: virtual public IDrawable {
public:
virtual ~IExtendedDrawable();
virtual void doSomethingElse() = 0;
};

class DrawableImplementation: virtual public IDrawable {
public:
virtual ~DrawableImplementation();
virtual void doSomething() {/*code here*/}
};

class ExtendedDrawableImplementation:
public DrawableImplementation, public IExtendedDrawable
{
public:
virtual ~ExtendedDrawableImplementation();
virtual void doSomething() {/*code here*/}
virtual void doSomethingElse() {/*code here*/}
};

关于c++ - 如何正确扩展接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12061719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com