mardi 27 février 2018

C++ sharing object between two class


Suppose I have two class: FirstContainer and SecondContainer
They hold the same reference (pass in their constructor) to the class ThirdContainer.
FirstContainer and SecondContainer have the following methods:
- addMemberToThirdContainer
- delMemberOfThirdContainer
- getThirdContainerMembers

When the ThirdContainer is modify, I want the two others to update using getThirdContainerMembers.
For this purpose I use the observer/listener pattern.

ThirdContainer has a list of listener and a method: addNewListener. FirstContainer and SecondContainer register by doing: mThirdContainer.addNewListener(this);

I'm not sure this is the good way to do this, I'm new to oriented object.
What are my other possibilities to achieve something like that ?
I'm not attached to anything I stated, it's just here because I have hard time to explain what I want to do. I hope it's clear. Thanks for the help,
Best,

Aucun commentaire:

Enregistrer un commentaire