11

The following code generates warning C4250. My question is, what's the best solution to it?

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
}

According to what I've read it should be possible to do this:

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

But, this doesn't actually do anything. The way I've currently solved it is:

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

What's everyone's view on this?

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • The code in the first block doesn't compile and after changing it to compile it doesn't generate C4250. – quamrana Feb 03 '10 at 08:53

5 Answers5

20

I had the same warning for the following code:

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.

David Segonds
  • 83,345
  • 10
  • 45
  • 66
Dani van der Meer
  • 6,169
  • 3
  • 26
  • 45
8

Did you try to inherit public virtual from class A? I think it should solve it.


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

The virtual inheritance suppose to solve the ambiguity.

Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
  • 3
    Yes, even with virtual inheritance it still has the same problem (there are two suitable functions to call, the compiler just chooses the most "overridden" one) – Mark Ingram Jan 22 '09 at 15:48
  • Then I guess there's no escape from your suggested solution. – Gal Goldman Jan 22 '09 at 15:49
  • 5
    I compiled your code now in VS2005 and without the virtual it's actually an error, not a warning. With the virtual it's a warning. – Gal Goldman Jan 22 '09 at 15:55
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.

Richard Lang
  • 456
  • 4
  • 15
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
}
0

I think the solution you're using may be the way to go, sorry to say. The only thing I can think of that might help is if you're able to make A's func1 pure virtual. That might not be feasible in your real program, though.

Evan Shaw
  • 23,839
  • 7
  • 70
  • 61