开发者

C++ multiple inheritance resolution issue with Visual Studio 2010 results in C2509 error

I'm trying to do something like C# explicit interface implementation in unmanaged C++. I am able to do it so long as the function implementation is part of the class definition but I can't figure out how to get it to work with the function implementation in the source file.

Here's my test code:

class Base
{
public:

    virtual void    Func() = 0;

    void    CallFunc()
    {
        Func();
    }
};

class Base1 : public Base
{
public:

    virtual void    Func() = 0;
};

class Base2 : public Base
{
public:

    virtual void    Func() = 0;
};

// This Works

class Multi : public Base1, public Base2
{
public:

    virtual void    Base1::Func()
    {
        puts("Base1::Func()");
    }

    virtual void    Base2::Func()
    {
        puts("Base2::Func()");
    }
};


// This does not

//  1>multipe开发者_如何学编程inheritancetest.cpp(60): error C2509: 'Func' : member function not declared in 'Multi'
//  1>          multipeinheritancetest.cpp(51) : see declaration of 'Multi'
//  1>multipeinheritancetest.cpp(65): error C2509: 'Func' : member function not declared in 'Multi'
//  1>          multipeinheritancetest.cpp(51) : see declaration of 'Multi'
/*
class Multi : public Base1, public Base2
{
public:

    virtual void    Base1::Func();
    virtual void    Base2::Func();
};

void Multi::Base1::Func()
{
    puts("Base1::Func()");
}

void Multi::Base2::Func()
{
    puts("Base2::Func()");
}
*/

// This is a work-around for when I don't want to declare the full function in the header
/*
class Multi : public Base1, public Base2
{
public:

    virtual void    Base1::Func() { Base1_Func(); }
    virtual void    Base2::Func() { Base2_Func(); }

    void    Base1_Func();
    void    Base2_Func();
};

void Multi::Base1_Func()
{
    puts("Base1_Func()");
}

void Multi::Base2_Func()
{
    puts("Base2_Func()");
}
*/

void RunMultiTest()
{
    Multi m;
    m.Base1::CallFunc();
    m.Base2::CallFunc();
}

Is there a way to do this and my syntax is just off or is this a Visual Studio bug or is this just not possible with C++?


The Base1::Func isn't valid in your "works" case either, it's just accepted by your compiler.

If you want two different functions call them two different names. If you want one function, use virtual inheritance:

EDIT: fixed from comment class Base1 : public virtual Base and class Base2 : public virtual Base

and then remove the base qualification from the function override.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜