Visual Studio 编译器警告 C4250(“class1":通过优势继承“class2::member")
以下代码生成警告 C4250.我的问题是,最好的解决方案是什么?
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(); }
}
大家对此有何看法?
推荐答案
我对以下代码有同样的警告:
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() {};
};
msdn 中 Visual C++ 2005 的错误报告建议这是一个已知的错误,被认为不足以修复......他们建议在这种情况下通过使用编译指示禁用警告.我认为这对您来说也是安全的,但您应该使用虚拟继承,如 Gal Goldman 的回答所示.
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.
相关文章