关于多重继承和定义虚函数

发布于 2024-11-05 04:58:03 字数 1122 浏览 10 评论 0原文

我有一个没有虚拟基类的多重继承场景,如下所示:

 Ta  Tb
 |   |
 B   C
  \ /
   A

Ta 和 Tb 是两个不同的模板类,它们都声明一个名为 f() 的虚拟函数。我想重写 A 范围中的两个函数,因为我必须在这些方法中与 B 和 C 数据进行交互。但我不知道该怎么做。

class Tb {
protected:
    virtual void f() {};
public:
    void call() {
        this->f();
    };  
};

class Tc {
protected:
    virtual void f() {};
public:
    void call() {
        this->f();
    };
};

class B : public Tb {
public:
    void doSomething() {};
};

class C : public Tc {
private:
    int c;
public:
    void inc() { c++; };
};

class A : public B, public C {
protected:
    void f() { // this is legal but I don't want to override both with the same definition.
        // code here
    }
    // if Tb::f() is called then i want to call C::inc()
    // if Tc::f() is called then i want to call B::doSomething()
public:
    void call() {
        B::call();
        C::call();
    };
};

是否有语法可以覆盖具有不同定义的方法,或者我是否必须在 B 和 C 中定义这些方法?

谢谢

编辑: 我的问题不是不能调用 Tb::f() 或 Tc::f(),而是我想在调用 Tb::f() 或 Tc::f() 时定义两种不同的行为。这些方法由 Tb 和 Tc 本身在其自己的公共方法中调用。 修改了示例,这样也许更清楚我想要做什么......

I have a multiple inheritance scenario without virtual base classes like this:

 Ta  Tb
 |   |
 B   C
  \ /
   A

Ta and Tb are two different template classes that both declare a virtual function named f(). I want to override the two function in the A scope, because I have to interact with both B and C data in these methods. But I don't know how to do this.

class Tb {
protected:
    virtual void f() {};
public:
    void call() {
        this->f();
    };  
};

class Tc {
protected:
    virtual void f() {};
public:
    void call() {
        this->f();
    };
};

class B : public Tb {
public:
    void doSomething() {};
};

class C : public Tc {
private:
    int c;
public:
    void inc() { c++; };
};

class A : public B, public C {
protected:
    void f() { // this is legal but I don't want to override both with the same definition.
        // code here
    }
    // if Tb::f() is called then i want to call C::inc()
    // if Tc::f() is called then i want to call B::doSomething()
public:
    void call() {
        B::call();
        C::call();
    };
};

Is there a syntax to override both the methods with different definitions or do I have to define these in B and C?

Thanks

Edit:
My problem is not that a cant call Tb::f() or Tc::f(), but that i want to define two different behavior if Tb::f() or Tc::f() is called. These methods are called by Tb and Tc itself in its own public methods.
Modified the example so maybe is more clear what i want to do...

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

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

发布评论

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

评论(3

笑脸一如从前 2024-11-12 04:58:03

是否有语法可以覆盖具有不同定义的方法,或者我是否必须在 B 和 C 中定义这些方法?

简短的回答:不。

您的 A:f() 将覆盖 Ta::f()Tb::f()

如果您想要不同的覆盖,唯一的解决方案是插入辅助类 Hb 和 Hc:

Ta  Tb
|   |
B   C
|   |
Hb  Hc
 \ /
  A 

Hb 可以小到两个函数,只需重命名该函数:

class Hb: public B {
   protected: // overrides from Ta
     virtual void f() { Ta_f(); }

   protected: // new virtuals
     virtual void Ta_f() = 0;
 };

然后您可以定义 A: :Ta_f() 做任何你想做的事情(包括调用 B::f() !)

Is there a syntax to override both the methods with different definitions or do I have to define these in B and C?

Short answer: no.

Your A:f() will override both Ta::f() and Tb::f().

If you want different overrides, the only solution is to insert helper classes Hb and Hc:

Ta  Tb
|   |
B   C
|   |
Hb  Hc
 \ /
  A 

Hb can be as small as two functions, just to rename the function:

class Hb: public B {
   protected: // overrides from Ta
     virtual void f() { Ta_f(); }

   protected: // new virtuals
     virtual void Ta_f() = 0;
 };

and then you can define A::Ta_f() to do whatever you want (including calling B::f() ! )

禾厶谷欠 2024-11-12 04:58:03

您确实在 A 类中重写了这两个内容。如果不这样做,就会出现歧义,除非使用 using 指令来明确指定您喜欢的方法。

您可以在 BC 类中分别重写这些方法,但同样,一旦 A 类继承了这两个类,您就必须决定应该使用哪一个。

You did override both in class A. If you didn't, there would have been an ambiguity unless using directive is used to explicitly specify what method of those you prefer.

You can override those methods separately in B and C classes, but again, once A class inherits both you have to decide which one should be used.

执手闯天涯 2024-11-12 04:58:03

您是否一定能够用 A 替换 TbTc ?如果没有,您是否考虑过在 A 中使用组合而不是继承?

如果您需要以这种方式替换,您是否考虑过采取简单的方法,仅将父方法称为两个不同的名称?由于您想单独覆盖它们,因此表明它们没有做同样的事情。

Do you absolutely have to be able to substitute an A for either a Tb or a Tc? If not, have you considered using composition in A instead of inheritance?

If you need to substitute in such a way, have you considered taking the easy way and just calling the parent methods two different names? Since you want to override them separately the indication is that they aren't doing the same thing.

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