Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c++ - Visual Studio Compiler warning C4250 ('class1' : inherits 'class2::member' via dominance)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...