Visual Studio 编译器警告 C4250(“class1”:通过支配继承“class2::member”)

发布于 2024-07-13 00:48:11 字数 528 浏览 4 评论 0原文

以下代码生成警告 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感悟人生的甜 2024-07-20 00:48:11

我对以下代码有相同的警告:

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 的答案所示。

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() {};
}; 

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.

猫弦 2024-07-20 00:48:11

你是否尝试从A类继承public virtual? 我认为应该解决它。


    class B :public virtual A;
    class C :public virtual A;
    class D : public virtual B, public virtual C;

虚拟继承旨在解决歧义。

Did you try to inherit public virtual from class A? I think it should solve it.


    class B :public virtual A;
    class C :public virtual A;
    class D : public virtual B, public virtual C;

The virtual inheritance suppose to solve the ambiguity.

妄司 2024-07-20 00:48:11

[确实是一条评论,但我没有足够的代表...]

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.

灵芸 2024-07-20 00:48:11

抱歉,我认为您正在使用的解决方案可能是可行的方法。 我能想到的唯一可能有帮助的是,如果你能够使 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.

伤感在游骋 2024-07-20 00:48:11

轻松解决

class A
{
  virtual void func1();
}

class B : public A
{
}

class C : public A
{
  virtual void func1();
}

class D : public B, public C
{
  virtual void func1()
  {
    C::func1(); 
  }
}

int main()
{
  D d;
  d.func1(); // Causes warning
}

Easy solve

class A
{
  virtual void func1();
}

class B : public A
{
}

class C : public A
{
  virtual void func1();
}

class D : public B, public C
{
  virtual void func1()
  {
    C::func1(); 
  }
}

int main()
{
  D d;
  d.func1(); // Causes warning
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文