11

以下代码生成警告 C4250。我的问题是,最好的解决方案是什么?

class A
{
  virtual void func1();
}

class B : public A
{
}

class C : public A
{
  virtual void func1();
}

class D : public B, public C
{
}

int main()
{
  D d;
  d.func1(); // Causes warning
}

根据我所阅读的内容,应该可以这样做:

class D : public B, public C
{
  using B::func1();
}

但是,这实际上并没有做任何事情。我目前解决的方法是:

class D : public B, public C
{
  virtual void func1() { B::func1(); }
}

大家对此有何看法?

4

5 回答 5

20

我对以下代码有同样的警告:

class Interface
{
public:
    virtual void A() = 0;
};

class Implementation : public virtual Interface
{
public:
    virtual void A() {};
};

class ExtendedInterface : public virtual Interface
{
    virtual void B() = 0;
};

class ExtendedImplementation : public ExtendedInterface , public Implementation
{
public:
    virtual void B() {};
}; 

This bug report for Visual C++ 2005 in msdn suggests that this is a known bug that was considered not important enough to fix... They suggest to disable the warning in this case by using a pragma. I think it is safe also in your case, but you should use virtual inheritance as shown in the answer by Gal Goldman.

于 2009-02-01T07:29:20.220 回答
8

您是否尝试从 A 类继承 public virtual ?我认为它应该解决它。


    class B :public virtual A;
    class C :public virtual A;
    class D : public virtual B, public virtual C;

虚拟继承假设解决了歧义。

于 2009-01-22T15:41:23.743 回答
1

[A comment really, but I don't have enough rep...]

David Segonds identified this as a known bug in VS 2005, just tried his example code in VS 2008 and it exhibits the same problem.

于 2014-08-03T21:11:41.693 回答
0

我认为您正在使用的解决方案可能是要走的路,很抱歉。我能想到的唯一可能会有所帮助的是,如果您能够使 A 的 func1 成为纯虚拟的。不过,这在您的实际程序中可能不可行。

于 2009-01-22T15:54:32.717 回答
0

Easy solve

class A
{
  virtual void func1();
}

class B : public A
{
}

class C : public A
{
  virtual void func1();
}

class D : public B, public C
{
  virtual void func1()
  {
    C::func1(); 
  }
}

int main()
{
  D d;
  d.func1(); // Causes warning
}
于 2013-07-30T07:33:49.627 回答