扩展接口,而不必重新实现其功能

发布于 2025-01-12 22:22:44 字数 1437 浏览 2 评论 0原文

我在接口继承方面遇到了一些困难。我想要实现的是,我想扩展一个接口,同时不必通过重用父接口实现中的函数实现来重新实现其所有功能。这可能吗?

因此,假设我有两个接口,第二个(IB)从第一个(IA)继承,

class IA
{
    public:
        virtual void funcA() const = 0;
};

class IB : public IA
{
    public:
        virtual void funcB() const = 0;
};

class AImpl : public IA
{
    public:
        virtual void funcA() const override {std::cout << "funcA from AImpl" << std::endl;};
};

class BImpl: public IB
{
    public:
        virtual void funcA() const override {std::cout << "funcA from BImpl" << std::endl;};
        virtual void funcB() const override {std::cout << "funcB" << std::endl;};
};

int main()
{
    BImpl b = BImpl();
    b.funcA();
    b.funcB();
    return 0;
}

这显然给了我预期的输出

funcA from BImpl
funcB

但是,我更想要的是 BImpl 使用来自 AImpl 的 funcA 的实现(如如果它仅从 AImpl 继承):

funcA from AImpl
funcB

我尝试让 BImpl 从 IB 和 AImpl 继承,但这不起作用(如果我不再次实现 funcA,BImpl 就会变得抽象)。我不让 BImpl 仅从 AImpl 继承并添加 funcB 的原因是我希望有一个单独的接口 (IB) 来针对 BImpl 类型的对象进行编码,同时允许我调用 funcA无需将对象强制转换为 IA 类型(因此是接口继承)。

// 编辑开始(3 月 10 日)

也就是说,我希望我的业务逻辑看起来像这样

int main()
{
    IB* b = new BImpl();
    b->funcA();
    b->funcB();
    return 0;
}

,需要 IB 继承 IA

// 编辑结束

有什么想法吗?理想情况下,我什至不必将 BImpl 中的 funcA 作为 AImpl 中 funcA 的包装器(如上所述,就好像我单独从 AImpl 继承一样)

I'm struggling with interface inheritance a little. What I'm trying to achieve is that I would like to extend an interface, whilst not having to reimplement all of its functions by reusing the function implementations from an implementation of the parent interface. Is this even possible?

So suppose I have two interfaces, from which the second (IB) inherits from the first (IA)

class IA
{
    public:
        virtual void funcA() const = 0;
};

class IB : public IA
{
    public:
        virtual void funcB() const = 0;
};

class AImpl : public IA
{
    public:
        virtual void funcA() const override {std::cout << "funcA from AImpl" << std::endl;};
};

class BImpl: public IB
{
    public:
        virtual void funcA() const override {std::cout << "funcA from BImpl" << std::endl;};
        virtual void funcB() const override {std::cout << "funcB" << std::endl;};
};

int main()
{
    BImpl b = BImpl();
    b.funcA();
    b.funcB();
    return 0;
}

which obviously gives me the expected output

funcA from BImpl
funcB

However, what I'd rather like to have is that BImpl uses the implementation of funcA from AImpl (as if it would only inherit from AImpl):

funcA from AImpl
funcB

I tried to have BImpl inherit from IB and AImpl, but that dosn't work (BImpl becomes abstract if I don't implement funcA again). The reason why I don't have BImpl inherit only from AImpl and add funcB is that I'd like to have a separate interface (IB) to code against for objects of type BImpl which, at the same time, allows me to call funcA without casting the objects to type IA (hence the interface inheritance).

// EDIT START (10th March)

That is, I'd like my business logic to look like this

int main()
{
    IB* b = new BImpl();
    b->funcA();
    b->funcB();
    return 0;
}

which requires IB to inherit from IA.

// EDIT END

Any ideas? Ideally, I would not even have to make funcA in BImpl a wrapper for funcA in AImpl (as said, as if I was inheriting from AImpl alone)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

归途 2025-01-19 22:22:44

使用虚拟继承来利用函数支配规则< /a>.

我对支配地位的具体运作方式感到生疏,但在这种情况下它恰好做了正确的事情。

#include <iostream>

class IA
{
  public:
    virtual void funcA() const = 0;
};

class IB : public virtual IA
{
  public:
    virtual void funcB() const = 0;
};

class AImpl : public virtual IA
{
  public:
    void funcA() const override {std::cout << "funcA" << std::endl;};
};

class BImpl : public AImpl, public IB
{
  public:
    void funcB() const override {std::cout << "funcB" << std::endl;};
};

int main()
{
    BImpl b;
    b.funcA(); // funcA
    b.funcB(); // funcB
    return 0;
}

Use virtual inheritance to take advantage of function dominance rules.

I'm rusty on how exactly dominance works, but it happens to do the right thing in this case.

#include <iostream>

class IA
{
  public:
    virtual void funcA() const = 0;
};

class IB : public virtual IA
{
  public:
    virtual void funcB() const = 0;
};

class AImpl : public virtual IA
{
  public:
    void funcA() const override {std::cout << "funcA" << std::endl;};
};

class BImpl : public AImpl, public IB
{
  public:
    void funcB() const override {std::cout << "funcB" << std::endl;};
};

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