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-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01