Should component know its host?

K

Krice

There is a component class inside the host class:

class Host
{
Component *c;
....

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
h->Do_Something();
....

Somehow I think this is bad design, but how to avoid it?
 
M

mlimber

There is a component class inside the host class:

class Host
{
  Component *c;
...

In some cases Component needs to call the host and
I guess the only way is that you pass the host for it:

Component::Component(Host *h)
{
  h->Do_Something();
...

Somehow I think this is bad design, but how to avoid it?

You could use a generic callback mechanism. Check out
std::tr1::function if you have TR1 available or Boost.Function if you
don't. It would make your code look something like:

Component::Component( const std::tr1::function& prologFn )
{
prologFn();
// ...
}

Also similar is _Modern C++ Design_'s Loki::Function (http://loki-
lib.sourceforge.net).

Cheers! --M
 
K

Krice

You could extract the behaviour the component needs into a separate
class and derive your host from that class.

I want to avoid deriving.. anyway, the solution was simple
after all. I wanted to control the host from the component,
but I guess it should be the other way around so now the host
is controlling the (now simplified) component.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top