#include <iostream>
struct B1
{
virtual void method()=0;
virtual ~B1(){}
};
struct B2
{
virtual void method()=0;
virtual ~B2(){}
};
struct D: B1, B2
{
virtual void method()
{
std::cout << "D::method\n";
};
};
int main(int argc,char *argv[])
{
D d;
B1 &b1=d;
B2 &b2=d;
b1.method();
b2.method();
return 0;
}
Note, B1 and B2 do not share common interface.
Is it this legal? If yes – in which standard? C++98/03/11 ?
Both, msvc and gcc have compiled it OK.
Previously I thought, that I have to use some common interface for such case (possible virtual inheritence).
Does such situation have some special name?
How it works in details, please? Maybe some ISO references?
Your code is well-formed:
void D::method()overrides bothvoid B1::method()andvoid B2::method().The specification states (C++11 §10.3/2):
B1declares a virtual member functionvoid B1::method(). ClassDis derived fromB1and it also declares a member function with the same name (method), the same parameter list (no parameters), the same cv-qualification (no qualification) and the same ref-qualifier (no qualification).Therefore,
void D::method()overridesvoid B1::method().The same logic applies for
void B2::method()(just substituteB2forB1in the above explanation), sovoid D::method()overrides bothvoid B1::method()andvoid B2::method().