c++ - Access protected function from derived class -
i have following typical scenario, in want hide implementation details in child class, , expose through interface:
template <typename derived> class interface { public: void a() { static_cast<derived*>(this)->_a(); } }; class implementation : public interface<implementation> { protected: void _a() { /* ... */ } };
i think understand why doesn't work, , know declaring class interface
friend of implementation
solves it, when comes more complex hierarchies, multiple interfaces, , various levels of inheritance(as real case), things messy.
i avoid having declare friend class interface<implementation>
in each class implements interface.
is there alternative nice-and-clean solution problem?
thanks!
how using virtual functions , polymorphism?
create object in child class , reassign interface class pointer or reference. create pure virtual function in interface class , define in child class.
Comments
Post a Comment