Y
Y
yourich2012-09-03 16:12:47
Programming
yourich, 2012-09-03 16:12:47

How to get rid of warning C4250: inherits via dominance?

Dear readers of habrahabr,
please help me with the following problem.
Let's say we have two interfaces:

class IA
{
public:
  virtual int FuncA() = 0;
};

class ISuperA : virtual public IA
{
public:
  virtual int FuncSuperA() = 0;
};

We make some kind of implementation for each of them:
class A1 : virtual public IA
{
public:
  virtual int FuncA() { return 1; }
};

class AIncrement: virtual public ISuperA
{
public:
  virtual int FuncSuperA() { return (FuncA() + 1); }
};

Then we create a class that inherits both interface implementations:
class A1Increment: virtual public AIncrement, virtual public A1
{
};

int main()
{
  A1Increment a;
  cout << "test: " << a.FuncSuperA() << endl;
}

In this case, when compiling in Microsoft Visual Studio 2010, the following warning occurs:
warning C4250: 'A1Increment': inherits 'A1::A1::FuncA' via dominance
The question arises: is it possible to get rid of this warning?
If this is not possible with this implementation, is it possible to use some other form of inheritance/interface?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
vScherba, 2012-09-03
@vScherba

If you don't want to suppress the warning, you can tell the compiler explicitly:

class A1Increment: public AIncrement, public A1
{
    virtual int FuncA()
    {
        return A1::FuncA();
    }
};

The fact is that the implementation of FuncA can be called in 2 ways a.AIncrement::FuncA() or a.A1::FuncA(). The calls are somewhat different. The 1st one calls AIncrement::ISuperA::IA::FuncA via the virtual table, and the 2nd one calls the implementation of A1::FuncA directly. The compiler honestly (so that without surprises) warned that the call to the 2nd option was built in.
By the way, for virtual inheritance of IA, it is enough to virtually inherit IA. The keyword virual is superfluous in the lines:

R
Ramires, 2012-09-03
@Ramires

About warning one and two . You can remove it with the warning pragma .
GCC and Clang didn't show any warning.

V
vScherba, 2012-09-03
@vScherba

class AIncrement: virtual public ISuperA
class A1Increment: virtual public AIncrement, virtual public A1

V
vScherba, 2012-09-03
@vScherba

I completely forgot: if you do not have data members in the base class, then you should not inherit it virtually. Normal inheritance will increase performance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question