C inheritance from multiple base classes with the same virtual function name

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;< 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~~

The function overrides the virtual function of the base class based on 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 two:

[class.virtual] / 2

If a virtual member function vf< /code> is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared , then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::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:

#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 class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list, cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::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 />}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2184 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.