### Two different pure virtuals with same name

Is there any way to define C?
 ``12345678910111213141516171819202122232425`` ``````struct A { virtual ~A(){} virtual void f() = 0; }; struct B { virtual ~B(){} virtual int f() = 0; }; struct C : virtual A, virtual B { virtual ~C(){} virtual void A::f() //error { std::cout << "C::A::f" << std::endl; } virtual int B::f() //error { std::cout << "C::B::f" << std::endl; return 7; } };``````
the problem here is that you are trying to implement a definition for functions A::f() and B::f() but these are pure virtual functions and therefore a class that inherit from struct A and struct B has to provide its own implementation for those two functions. so on line #16 and line#20, you can not provide an implementation for pure virtual functions (line# 4 and line #10).
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include struct A { virtual ~A(){} virtual void f() = 0; }; struct B { virtual ~B(){} virtual int f() = 0; }; struct A_1 : virtual A // intermediate class to disambiguate f() { virtual void f() override { A_f() ; } // map f() to A_f() protected : virtual void A_f() = 0 ; }; struct B_1 : virtual B // intermediate class to disambiguate f() { virtual int f() override { return B_f() ; } // map f() to B_f() protected : virtual int B_f() = 0 ; }; struct C : virtual A_1, virtual B_1 { // do not override f() protected: // override A_f() and B_f() virtual void A_f() override { std::cout << "C::A::f" << std::endl; } virtual int B_f() override { std::cout << "C::B::f" << std::endl; return 7; } }; int main() { C c ; A& a = c ; a.f() ; // C::A::f (C::A_f) B& b = c ; b.f() ; // C::B::f (C::B_f) // c.f() ; // *** error: ambiguous }``````
Last edited on
Ah, I knew there had to be some intermediate magic.

I can still do c.A_1::f and c.B_1::f too, which is nice. Thanks!
Last edited on
Topic archived. No new replies allowed.