dynamic inheritance without touching classes
Posted
by Jasper
on Stack Overflow
See other posts from Stack Overflow
or by Jasper
Published on 2010-05-16T20:53:41Z
Indexed on
2010/05/16
21:00 UTC
Read the original article
Hit count: 199
c++
|inheritence
I feel like the answer to this question is really simple, but I really am having trouble finding it. So here goes:
Suppose you have the following classes:
class Base;
class Child : public Base;
class Displayer
{
public:
Displayer(Base* element);
Displayer(Child* element);
}
Additionally, I have a Base* object
which might point to either an instance of the class Base
or an instance of the class Child
.
Now I want to create a Displayer
based on the element pointed to by object
, however, I want to pick the right version of the constructor. As I currently have it, this would accomplish just that (I am being a bit fuzzy with my C++ here, but I think this the clearest way)
object->createDisplayer();
virtual void Base::createDisplayer()
{
new Displayer(this);
}
virtual void Child::createDisplayer()
{
new Displayer(this);
}
This works, however, there is a problem with this:
Base
and Child
are part of the application system, while Displayer
is part of the GUI system. I want to build the GUI system independently of the Application system, so that it is easy to replace the GUI. This means that Base
and Child
should not know about Displayer
. However, I do not know how I can achieve this without letting the Application classes know about the GUI.
Am I missing something very obvious or am I trying something that is not possible?
© Stack Overflow or respective owner