C++ Visual Studio 2010 的多重继承解决问题导致 C2509 错误
我正在尝试在非托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.