在其他父分支中实现虚拟方法而不是在其中声明的虚拟方法

发布于 2024-11-28 02:26:43 字数 989 浏览 0 评论 0原文

我有以下代码:

#include <iostream>

class Grandma
{
public:
    virtual void foo() = 0;
};

class Mom : public Grandma{};

class Dad
{
public:
    void foo() { std::cout << "dad's foo impl"; }
};

class Me : public Mom, public Dad{};

int main()
{
    Me m;
    m.foo();
}

并得到: 无法实例化抽象类

我知道这个错误意味着什么,我知道由于纯虚拟,我无法实例化 Grandma。

但是,当编译器知道我是从妈妈和爸爸派生并且爸爸已经实现了 foo 时,为什么我不能实例化 Me?

我知道我可以通过将 foo 添加到 Me 中并在其中调用例如 Dad::foo 来修复它,但恐怕这不是解决方案对于我的情况。

是否真的有必要在其声明和实例化对象之间实现虚拟方法(当遍历“类层次结构图”时)?请参阅 ASCI 图

    A
    |
B1  B2
  \ |   
   C1  C2
    | /
    |/
    D
    |
    E

当我想实例化 E 并在 A 中进行虚拟声明时,使其运行的唯一方法是在 A、B2、C1、D 或 E 中定义它? 同样,C2 中的虚拟声明什么时候唯一的方法是在 C2、D 或 E 中定义它?

我知道这可能是个愚蠢的问题,但我度过了漫长的一天,无法再思考。

请不要仅仅回答“这是不可能的”,而是尝试添加解释为什么不可以。

谢谢你!

编辑 - Dad 中的 foo() 当然不应该是私有的

I have the following code:

#include <iostream>

class Grandma
{
public:
    virtual void foo() = 0;
};

class Mom : public Grandma{};

class Dad
{
public:
    void foo() { std::cout << "dad's foo impl"; }
};

class Me : public Mom, public Dad{};

int main()
{
    Me m;
    m.foo();
}

and getting : cannot instantiate abstract class

I know what this error means, I know I can't instantiate Grandma because of pure-virtual.

But why can't I instantiate Me, when compiler knows I am derived from Mom and Dad and Dad has foo implemented?

I know I can fixed it by adding foo into Me and inside it call e.g. Dad::foo, but I am afraid it is not solution for my case.

Is it really necessary to have virtual method implementation between its declaration and instantiated object (when traversing "class hierarchy graph")? See ASCI graph

    A
    |
B1  B2
  \ |   
   C1  C2
    | /
    |/
    D
    |
    E

When I want to instantiate E and have virtual declaration in A, the only way to make it run is to define it in A, B2, C1, D or E?
and similarly when is virtual declaration in C2 only way is to define it in C2, D or E?

I know this may be silly question, but I had a loooong day and can not think anymore.

Please, do not answer just with - "It is not possible", but try to add explanation why not.

Thank you!

EDIT -- foo() in Dad is of course should not be private

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

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

发布评论

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

评论(3

萌化 2024-12-05 02:26:43

Dad 类和 Grandma 类之间没有关系DadGrandma 是两个完全不同的类。鉴于 Dad 中的方法不被视为 Grandma 中纯虚方法的实现,而必须在 Me 类中实现。

当您从抽象类(包含至少一个纯虚函数的类)派生时,派生类需要重写并实现基类的所有纯虚成员函数。如果不是,派生类也成为抽象类,这同样适用于从派生类进一步向下派生的类。

There is no relation between Dad class and Grandma class. Dad and Grandma are two completely different classes. Given that the method in Dad is not considered as an implementation of the pure virtual method in Grandma and has to be implemented in the Me class.

When you derive from an Abstract class(class containing atleast one pure virtual function) the deriving class needs to override and implement ALL the pure virtual member functions of the Base Class. If not, the deriving class becomes Abstract too, same applies to the classes deriving from the derived class further down the hierarchy.

关于从前 2024-12-05 02:26:43

不幸的是,您是对的,您必须在原始声明和最终实例化对象之间的路径中实现虚拟方法。多重继承的对象无法实现该接口。

因此,从这个问题来看,您似乎正在尝试通过从实现者继承来在子类中实现父接口。您是否考虑过让父类将工作委托给策略模式,然后在子类中选择正确的策略,而不是采用这种方法。然后您仍然可以一起组合实现,而不必担心多重继承的许多复杂性。

Unfortunately you're right that you have to implement the virtual method in the path between original declaration and final instantiated object. Objects multiply inherited in can't implement that interface.

So from the question it sounds like you're trying to implement a parent interface in a child class by inheriting from the implementor. Instead of that approach, did you consider instead having the parent class delegate the work to a strategy pattern and then select the proper strategy in your child class. Then you can still compose implementations together and don't have to worry about the many complexities of multiple inheritance.

与风相奔跑 2024-12-05 02:26:43

您是正确的,虚拟函数只能被从最初声明虚拟函数的类继承(可能通过多个层)的类提供的实现覆盖。

但是,根据您实际想要完成的任务,姐妹类委托可能是你想要的。基本上,您可以在奶奶和爸爸(“人”?)共同的基础中声明虚拟函数,并且奶奶可以调用这些函数,即使当您使用 Me 对象时只有爸爸提供实现。尽管奶奶对爸爸一无所知,但当您使用虚拟继承时,您的最终类引入的任何子类都可以用于满足虚拟基类中的虚拟函数。

You are correct that a virtual function is only overridden by implementations provided by classes inheriting (potentially through several layers) from the class where the virtual function is originally declared.

However, depending on what you are actually trying to accomplish, Sister Class Delegation may what you want. Basically, you can declare virtual functions in a base common to both Grandma and Dad ('Person'?) and Grandma can call those functions even if only Dad provides an implementation when you're using a Me object. Even though Grandma would know nothing about Dad, when you use virtual inheritance any subclasses your final class pulls in can be used to satisfy virtual functions from the virtual base class.

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