Visual Studio 编译器警告 C4250(“class1”:通过支配继承“class2::member”)
以下代码生成警告 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(); }
}
大家对此有何看法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我对以下代码有相同的警告:
此错误报告 msdn 中的 Visual C++ 2005 表明这是一个已知的错误,被认为不够重要,无法修复...他们建议在这种情况下通过使用编译指示来禁用警告。 我认为在你的情况下也是安全的,但你应该使用虚拟继承,如 Gal Goldman 的答案所示。
I had the same warning for the following code:
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.
你是否尝试从A类继承public virtual? 我认为应该解决它。
虚拟继承旨在解决歧义。
Did you try to inherit public virtual from class A? I think it should solve it.
The virtual inheritance suppose to solve the ambiguity.
[确实是一条评论,但我没有足够的代表...]
David Segonds 认为这是 VS 2005 中的一个已知错误,只是在 VS 2008 中尝试了他的示例代码,它也出现了同样的问题。
[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.
抱歉,我认为您正在使用的解决方案可能是可行的方法。 我能想到的唯一可能有帮助的是,如果你能够使 A 的 func1 成为纯虚拟的。 但这在您的实际程序中可能不可行。
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.
轻松解决
Easy solve