oop - Implementing multiple interfaces in c++ -
i have interface hierarchy follows:
class { public: void foo() = 0; }; class b: public { public: void testb() = 0; }; class c: public { public: void testc() = 0; }; now, want implement these interfaces same hierarchy, base class aimpl, bimpl , cimpl not sure how derive them corresponding interfaces.
please help. in advance.
you can implement each individial interface using separate template , chain templates construct derived object if building blocks. method used venerable atl library implement com interfaces (for of old enough).
note don't need virtual inheritance that.
i modified example more complex derivation c -> b -> a show how method scales easily:
#include <stdio.h> // interfaces struct { virtual void foo() = 0; }; struct b : { virtual void testb() = 0; }; struct c : b { virtual void testc() = 0; }; // implementations template<class i> struct aimpl : { void foo() { printf("%s\n", __pretty_function__); } }; template<class i> struct bimpl : { void testb() { printf("%s\n", __pretty_function__); } }; template<class i> struct cimpl : { void testc() { printf("%s\n", __pretty_function__); } }; // usage int main() { // compose derived objects templates building blocks. aimpl<a> a; bimpl<aimpl<b> > b; cimpl<bimpl<aimpl<c> > > c; a.foo(); b.foo(); b.testb(); c.foo(); c.testb(); c.testc(); } output:
void aimpl<i>::foo() [with = a] void aimpl<i>::foo() [with = b] void bimpl<i>::testb() [with = aimpl<b>] void aimpl<i>::foo() [with = c] void bimpl<i>::testb() [with = aimpl<c>] void cimpl<i>::testc() [with = bimpl<aimpl<c> >]
Comments
Post a Comment