Archived

This topic is now archived and is closed to further replies.

sprinter_trueno

a matter of hierarchy ?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

// ..


// Getter

CInterrupt* GetTimerInt();
CInterrupt* GetSoundInt();
CInterrupt* GetVideoInt();

// The interrupt objects

private:
CInterrupt* pTimerInterrupt; // To be passed to the timer object

CInterrupt* pVideoInterrupt; // To be passed to the video object

CInterrupt* pSoundInterrupt; // To be passed to the sound object

}


Can''t help it. I just don''t get along with it. What do you think ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites