class A
{
virtual void foo( ) = 0;
};
class B
{
virtual void foo() = 0;
};
class C: public A, public B
{
//virtual void A::foo(){}
//virtual void B::foo(){}
virtual void A::foo();
virtual void B::foo();
};
void C::A::foo() {}
void C::B::foo(){}
int main()
{
C c;
return 0;< br />)
It is ok when using the comment part, but when I try to write the definition outside the class declaration, the compiler reports an error.
I am using the MSVC11 compiler, does anyone know how Write this?
I need to move the code to the cpp file.
Thank you~~
[class.virtual] / 2
If a virtual member function
vf< /code> is declared in a class
Base
and in a classDerived
, derived directly or indirectly fromBase
, a member functionvf
with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) asBase::vf
is declared , thenDerived::vf
is also virtual (whether or not it is so declared) and it overridesBase::vf
.
As I have already said in the comments, [dcl.meaning]/1 prohibits the use of qualified-id in the declaration of (member) functions:
When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifi er refers […]”
So any virtual emptiness X :: foo(); as a declaration inside C is illegal.
Code
class C: public A, public B
{
virtual void foo();
};
It is AFAIK to cover foo The only way, it will overwrite A::foo and B::foo. For A::foo and B::foo, there is no way to overwrite them in two different ways other than introducing another inheritance layer: p>
#include
struct A
{
virtual void foo() = 0;
};
struct B
{
virtual void foo() = 0;
};
struct CA: A
{
virtual void foo() {std::cout << "A" << std::endl; }
};
struct CB: B
{
virtual void foo() {std::cout << "B" << std::endl; }
};
struct C: CA, CB {};
int main() {
C c;
//c.foo(); // ambiguous
A& a = c;
a.foo();
B& b = c;
b.foo();
}
I tried this code:
class A
{
virtual void foo() = 0;
};
class B
{
virtual void foo() = 0;
};
class C: public A, public B
{
//virtual void A::foo(){}
//virtual void B::foo(){}
virtual void A::foo();
virtual void B::foo();
};
void C::A::foo(){}
void C::B::foo(){}
int main()
{
C c;
return 0;
}
It is ok when using the comment section, but when I try to write the definition outside the class declaration, The compiler will report an error.
I am using the MSVC11 compiler, does anyone know how to write this?
I need to move the code to the cpp file.
Thank you~~
The function overrides the base class according to the name and parameter type (See below). Therefore, your class C has two virtual functions foo, one inherits from each of A and B. But the function void C :: foo() will cover both:
[class.virtual] / 2
If a virtual member function
vf
is declared in a classBase
and in a classDerived
, derived directly or indirectly fromBase
, a member functionvf
with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) asBase::vf
is declared, thenDerived::vf
is also virtual (whether or not it is so declared) and it overridesBase::vf
.
As I am commenting As already mentioned in [dcl.meaning]/1, it is prohibited to use qualified-id in the declaration of (member) functions:
When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers […]”
Therefore any virtual void X ::foo(); as C internal The declaration is illegal.
Code
class C: public A, public B
{
virtual void foo() ;
};
is AFAIK the only way to overwrite foo, it will overwrite A::foo and B::foo. For A::foo and B::foo, except for introducing another Outside the inheritance layer, there is no way to cover it in two different ways:
#include
struct A
{
virtual void foo() = 0;
};
struct B
{
virtual void foo() = 0;
} ;
struct CA: A
{
virtual void foo() {std::cout << "A" << std::endl; }
} ;
struct CB: B
{
virtual void foo() {std::cout << "B" << std::endl; }
} ;
struct C: CA, CB {};
int main() {
C c;
//c.foo(); // ambiguous
A& a = c;
a.foo();
B& b = c;
b.foo();< br />}