c++ call private function in container class -
c++ call private function in container class -
if have 2 classes:
class worker { top *mp_parent; worker(top *parent) : mp_parent(parent) {}; int dosomework() { int = mp_parent->privatefunction(); // function want phone call } } class top { private: worker m_worker; int privatefunction() {return 1;} }
where top class contains instance of worker class. when worker instantiated, pointer parent class passed in. later, function dosomework() called needs value parent, calls mp_parent->privatefunction().
what best way accomplish this? - don't want create privatefunction() public function if can avoid it, not work because private :o
are there other options?
maybe can utilize 'friend' keyword:
class worker { top *mp_parent; worker(top *parent) : mp_parent(parent) {}; int dosomework() { int = mp_parent->privatefunction(); // function want phone call } } class top { friend class worker; private: worker m_worker; int privatefunction() {return 1;} }
c++ container-classes
Comments
Post a Comment