C++ Visual Studio 2010 的多重继承解决问题导致 C2509 错误

发布于 2024-10-19 09:27:14 字数 1827 浏览 1 评论 0原文

我正在尝试在非托管 C++ 中执行类似 C# 显式接口实现的操作。只要函数实现是类定义的一部分,我就可以做到这一点,但我不知道如何让它与源文件中的函数实现一起工作。

这是我的测试代码:

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

有没有办法做到这一点,并且我的语法刚刚关闭,或者这是一个 Visual Studio 错误,还是这对于 C++ 来说是不可能的?

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>multipeinheritancetest.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++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

污味仙女 2024-10-26 09:27:14

Base1::Func 在您的“工作”情况下也无效,它只是被您的编译器接受。

如果您想要两个不同的函数,请给它们起两个不同的名称。如果您想要一个函数,请使用虚拟继承:

编辑:从注释中修复
类 Base1:公共虚拟基

class Base2 : public virtual Base

然后从函数重写中删除基本限定。

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.

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