Overloading virtual methods in base classes

Visual Studio 2013.

Given:

class base_1
{
public:
    virtual void foo(int) = 0;
};

class base_2
{
public:
    virtual void foo(int, double) = 0;
};

class join_1_2 : public virtual base_1, public virtual base_2
{};

I have a sink:

void sink(join_1_2 &param)
{
    param.foo(42, 3.14);
}

But I get the following compiler errors:

error C2385: ambiguous access to 'foo'

may be "foo" in the database "base_1"

or maybe "foo" in the base_2 database

error C2660: 'base_1 :: foo': the function does not accept 2 arguments

error C3861: 'foo': identifier not found

I know that I can solve this problem with:

param.base_2::foo(42, 3.14);

But, as you can imagine, virtual inheritance is already one sin with which I have to live too much. I'm probably going to write an adapter. But I don’t understand what prevents the compiler from trying to resolve foo to base_2. My colleague believes that this is a compiler error, but I do not blame the provider so quickly.

++ ?

+4
2

, using :

class join_1_2 : public virtual base_1, public virtual base_2
{
public:
    using base_1::foo;
    using base_2::foo;
};

void sink(join_1_2 &param)
{
    param.base_2::foo(42, 3.14);
}

7.3.3

, - , .

+4

, - foo . , , -:

class join_1_2 : public virtual base_1, public virtual base_2
{
public:
    using base_1::foo;
    using base_2::foo;
};
+4

Source: https://habr.com/ru/post/1620444/


All Articles