Visual Studio Compiler warning C4250 (#39;class1#39; : inherits #39;class2::member#39; via dominance)(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.
这篇关于Visual Studio 编译器警告 C4250(“class1":通过优势继承“class2::member")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Visual Studio 编译器警告 C4250(“class1":通过优势继承“class2::member")
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01
- 从python回调到c++的选项 2022-11-16