c++ - Virtual parent and child relationship -


the code snippets underneath keyboard.h , keyboard.cpp. question is, how implement layered parent , child relationship? if child dose not implement of virtual members, parent may free so. example, child implements keyboarddepressa dose not implement keyboarddepressb, leaving parent implement catch keyboarddepressb. parent , child of course of different types , not...

i don't want child know parents' support device::keyboard; therefor, wanted call parent using instance see in keyboarddepressb. in order make work, need assign parent static instance static dose not have support key word this. should do?

i kept code minimum. if more needed please feel free ask. ^^

class keyboard {  private:      keyboard ();     ~keyboard ();  public:      static device::keyboard& instance();     static void setparent(device::keyboard *cpparent);      virtual void    keyboarddepressa    ();     virtual void    keyboardreleasea    ();      virtual void    keyboarddepressb    ();     virtual void    keyboardreleaseb    ();      …   …   ... };   device::keyboard& keyboard::instance() {      static device::keyboard nrkeyboard;     return nrkeyboard;  } // member  void keyboard::setparent(device::keyboard *cpparent) {      device::keyboard& nrparent(keyboard::instance());     *this = cpparent;  } // member  void keyboard::keyboarddepressa() {      device::keyboard& nrparent(keyboard::instance());     nrparent.keyboarddepressa();  } // member  void keyboard::keyboardreleasea() {      device::keyboard& nrparent(keyboard::instance());     nrparent.keyboardreleasea();  } // member  void keyboard::keyboarddepressb() {      device::keyboard& nrparent(keyboard::instance());     nrparent.keyboarddepressb();  } // member  void keyboard::keyboardreleaseb() {      device::keyboard& nrparent(keyboard::instance());     nrparent.keyboardreleaseb();  } // member 

so, in general it's complicated mix inheritance singleton pattern cautious there.

if have parent child relationship, in c++ parent and/or child can implement virtual function.

if want parent implement , not child: declare virtual in parent , implement parent not declare or implement child

class parent { public:     virtual void func(); } void parent::func(){ ... } class child : public parent { }; parent* p = new child; p->func(); // calls parent's func 

if want parent implementation overridden child: declare virtual in both parent , child classes , implement both classes (note child can call parent's implementation without having understand implementation is)

class parent { public:     virtual void func(); } void parent::func(){ ... } class child : public parent {     virtual void func(); }; void child::func(){ ... parent::func();} parent* p = new child; p->func(); // calls child's func 

if want child implement , parent not: declare pure virtual member in parent , declare , implement child

class parent { public:     virtual void func() = 0; } class child : public parent {     virtual void func(); }; void child::func(){ ... } parent* p = new child; p->func(); // calls child's func 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -