Instead of inheriting, you could add data members to roundtrip
, but the functions in walk
and car
are currently protected
. If you cannot make the functions in walk
and car
public, you could:
befriend the
roundtrip
templateclass walk { protected: void move() { std::cout<<"i'm walking."<<std::endl; } template<class T, class S> friend class roundtrip; };
make the member functions accessible to
roundtrip
via a helper class:template<typename S, typename T> class roundtrip { private: struct helperT : private T { public: // or private + friend roundtrip using T::move; using T::T; } mT; struct helperS : private S { public: using S::move; using S::S; } mS; public: void printSchedule(void) { std::cout<<"away: "; mT.move(); std::cout<<"return: "; mS.move(); } };
If the interface is the same for both types, you could use a helper class template instead of two helper classes:
template<typename S, typename T> class roundtrip { private: template<class U> struct helper : private U { public: // or private + friend roundtrip using U::move; using U::U; }; helper<S> mS; helper<T> mT; public: void printSchedule(void) { std::cout<<"away: "; mT.move(); std::cout<<"return: "; mS.move(); } };
(it is also possible to inherit from a helper template specialization, e.g.
class roundtrip : helperT<T>, helperS<S>
)