دنبال کننده ها

۱۳۹۶ مهر ۹, یکشنبه

inheritance - How to properly delegate interface to sister class in C++?

[ad_1]



Let's assume I have classes Child and Parent, where Child extends the Parent:



class Parent 
void foo() cout << "foon";
void bar() cout << "barn";
void jar() cout << "jarn";
;

class Child: public Parent
void child_method1();
void child_method2();
;


Now I want to export some of the Child functionality using the following interface:



class Interface 
public:
virtual ~Interface()

virtual void foo() = 0;
virtual void bar() = 0;

protected:
Interface()
;


As you can see the interface methods are all already implemented in Parent. How do I properly define Child so that the Interface is delegated to the Parent ?



I'm trying to do it like:



class Child: public Parent, public Interface 
void child_method1();
void child_method2();



But the compiler complains that the virtual methods foo() and bar() are not implemented in Child.



If I write an explicit delegation like this:



class Child: public Parent, public Interface 
void foo() Parent::foo();
void bar() Parent::bar();

void child_method1();
void child_method2();



the compiler doesn't complain.
Is it necessary to write the explicit delegation? What is the proper way to delegate the virtual methods to a sister class?




[ad_2]

لینک منبع