a matter of hierarchy ?

Started by
2 comments, last by sprinter_trueno 20 years, 11 months ago
Hi ppl, The following situation happens to me over n over again. I have an object A that creates objects of class B. Now I have objects of class C that need references to objects B stored in A. What is a good way to pass them around ? Making a ''Getter'' for every reference feels quite awkward. Creating objects B before A and the C''s and then passing them at instantiation of A and C also doesn''t feel right since they shouldn''t be that global. What is a standard approach to this basic problem ? Tnx for any comment Lorenz
Advertisement
There's no standard approach. Well, maybe getters are. But what you *should* do depends highly on the specific problem. There may be some super-elegant solution you just haven't thought about yet but it's impossible to tell without full context. (Note: this is not an invitation to write the problem here, but to perhaps first solve it with getters and refactor it later into a better design if you come up with one)

[edited by - civguy on May 5, 2003 3:46:14 AM]
Thanks for the reply. Already eased my headace a little bit.

For my problem I''ll give you some information.

I have a class of type CInterruptHandler which will create objects of CInterrupt passing itself so that the interrupt objects know who their handler is (There might be more than one handler).

The interrupts will be passed to objects that can cause an interrupt. (Mediator pattern would be cool but some objects have more than one interrupt reference). The Interrupt handler doesn''t know and doesn''t need to know about the objects, only to which object it is passed to so that it can take proper actions.

A derived class of the handler would look like:

  class CInterrupt;class CIntHandlerDerived : CInterruptHandler{CIntHandlerDerived();~CIntHandlerDerived();// Some functions// ..// GetterCInterrupt* GetTimerInt();CInterrupt* GetSoundInt();CInterrupt* GetVideoInt();// The interrupt objectsprivate:CInterrupt* pTimerInterrupt; // To be passed to the timer objectCInterrupt* pVideoInterrupt; // To be passed to the video objectCInterrupt* pSoundInterrupt; // To be passed to the sound object}  


Can''t help it. I just don''t get along with it. What do you think ?
Looks great to me. Only thing is you may want to throw some consts in there so the CInterrupt objects are uneditable outside the class. However, if you do want outside classes to be able to edit the interrupts, then you might as well just make them public and forget the accessor functions (where did this ''getter'' phrase come from anyways?) altogether, unless, of course, you are doing some kind of checking in the accessor itself.

If you think accessors are bad in this setting, you should try writing a compiler some time.

This topic is closed to new replies.

Advertisement