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
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01