【问题标题】:Define which parent function has to be taken定义必须采用哪个父函数
【发布时间】:2013-11-08 08:57:27
【问题描述】:

我有一个子类,它派生自两个父类(一个公有,一个私有)。每个父类都有一个重载 operator* 的函数。如果我在子类中使用这个函数,我会得到一个错误(不明确的函数),但是我想使用公共父类的方法。

class ParentA
{
 public:
   ParentA operator*(const ParentA & other);
};

class ParentB
{
 public:
   ParentB operator*(const ParentB & other);
};

class Child : public ParentA, private ParentB
{
   ...
};

int main()
{
   Child x,y;
   x*y;
   return 0;
}

我该如何解决这个问题?

非常感谢, 雷莫

【问题讨论】:

  • 你可以试试ret_type ret = ParentA::operator*( parameter );

标签: c++ inheritance


【解决方案1】:

尝试如下编写子类(未经测试):

class Child : public ParentA, private ParentB {
public:
using ParentA::operator*;
... /*same as before*/
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多