c++ - OOP: Proper way to check other instances of objects' variables -


i have collection of related classes, call them

class manager {      private:        std::vector<setting> m_settings; }  class setting {     private:         std::vector<action> m_actions; }  class action {     private:         keybind m_keybind; }  class keybind {     public:         updatekeybind;     private:         type keybind; } 

as can see pseudo-c++ code, settings have actions, , actions have 1 key binding. now, user of application want update keybind potentially, yes?

i have buttons in keybind-type dialog associated each action, action can handle updating it's own keybind.

my problem:

how ensure keybinding isn't bound object?

possible solutions:

  1. move updatekeybind manager class, have manager query settings.
  2. have parent pointer in action/setting/keybind action can query manager updated keybind.
  3. have action query other actions (not great conceptually far can tell).

what need you:

  1. what rigorous approach, in terms of maintainability, speed, ease of understanding, , oop appropriateness, implementing checking if keybind found, whether out of suggested solutions or else entirely. have tried number 1 -- works, feel better, ya dig? unable find similar questions on stackoverflow, if i'd love see them!

  2. any other pro tips, things improve helpful.

just @amardeep says, can try creating class managing mapping between actions , keybindings. following example. automatically remove keybind action if there new binding keybind.

class manager {     private:        std::vector<setting*> m_settings;        keybindpool *m_keybindpool; };  class setting {     private:         std::vector<action*> m_actions; };  class action {     public:         void bind(keybind* keybind) {             m_manager->m_keybindpool->update(this, keybind)         }         keybind* getkeybind() const {             return m_manager->m_keybindpool->getkeybind(this);         }     private:         manager *m_manager; };  class keybindpool { public:     void update(action* action, keybind* keybind) {         if (m_keybindactionmap.find(keybind) != m_keybindactionmap.end()) {             m_actionkeybindmap.erase(action);         }         m_keybindactionmap[keybind] = action;         m_actionkeybindmap[action] = keybind;     }     keybind* getkeybind(action* action) {         return m_actionkeybindmap[action];     } private:     map<keybind*, action*> m_keybindactionmap;     map<action*, keybind*> m_actionkeybindmap; };  class keybind {     private:         type keybind; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -