I have a naive C++ inheritance class question. I have one base class Base, two derived classes DerivedA and DerivedB and two other distinct classes A and B which share the names of some methods.
Ideally I would like to design methods in the base class that use the methods of A and B. My attempt is the following
#include <cstdio> class A{ public : A(){}; void speak(){printf("hello A\n");}; }; class B{ public : B(){}; void speak(){printf("hello B\n");}; }; class Base{ public: Base(){}; void method(){property.speak();} A property; }; class DerivedA : public Base{ public: using Base::Base; A property; }; class DerivedB : public Base{ public: using Base::Base; B property; }; int main(int argc, char *argv[]){ DerivedA da; DerivedB db; da.property.speak(); db.property.speak(); // Attempting to call the shared method on the two distinct properties da.method(); db.method(); } Could you point me to a better design ? I suspect I should use templates but I am not sure how.