   Having two child objects communicate with each other
  Hi guys, I'm having somewhat of a dilemma. This is a very contrived example, but look:  Main Class  |   |  *   *    Sub-System A    Sub-System BOk, so I need Sub-system A to communicate with Sub-System B by way of accessing and calling its methods etc.The thing is, I want to make sure that S-S A uses the same instance of S-S B as the main class created and they continue to operate on the same instance.So, I was wondering if I were to pass the S-S B object to S-S A when it is created in Main, whether it would consistently remain the same object that both are changing and accessing? Or would I have to make use of a....*cough*singleton*cough*....?Programming in Java by the way.EDIT: Ok, so the formatting for the "diagram" didn't come out right. Basically, main class instantiated S-S A and S-S B. I need them to talk to each other.   
  Not sure how you'd write this in java..class MainClass{ shared_ptr<SSA> Sa; shared_ptr<SSB> Sb;public: MainClass()  : Sa(new SSA()), Sb(new SSB()) {   Sa->SetSSB(Sb);   Sb->SetSSA(Sa); }};That could lead to a potentially deadlocked cleanup couldnt it? Where the object at Sa wont get cleaned up because the object at Sb has a reference to it. And vise versa. Assuming they both are holding a strong reference. So MainClass would get destroyed and Sa and Sb would be forgotten about.. That's not good..Edit:If you add:~MainClass(){ Sa->SetSSB(shared_ptr<SSB>()); Sb->SetSSA(shared_ptr<SSA>());}You'd be alright.[Edited by - bzroom on June 25, 2009 4:11:15 PM];Quote:Original post by S-DawgSo, I was wondering if I were to pass the S-S B object to S-S A when it is created in Main, whether it would consistently remain the same object that both are changing and accessing? Or would I have to make use of a....*cough*singleton*cough*....?So long as you don't reassign the S-S B object variable in either S-S A or Main, both will hold references to the same object.  If you do try to reassign the S-S B object variable in either Main or S-S A, they will not store the same reference to the same S-S B object (unless you do something like SSBobject = SSBobject (i.e. assign the S-S B object to itself)).This all works because in Java variables store the reference to the object, not the object itself.   ;Quote:Original post by S-DawgSo, I was wondering if I were to pass the S-S B object to S-S A when it is created in Main, whether it would consistently remain the same object that both are changing and accessing?It would, since you're really only passing a reference, Main and SSA would both have different references both pointing at the same object instance.Quote:EDIT: Ok, so the formatting for the "diagram" didn't come out right.You can use <pre></pre> or [code][/code] tags to format text using spaces:  Main Class  |   |  *   *    Sub-System A    Sub-System B