Re: How to create an alias - overlaying two class hierarchies
On 8 oct, 12:45, Matt <m...@matthiasm.com> wrote:
Michael Doubez wrote:
Also, I'm not so sure about what TLC stands for, here, top-level
classes perhaps?
I think he means TCL (Tool Command Language) like tcl/tk.
Oh, sorry, TLC stands for "tender love and care". I meant to say that we
did not take care of the fltk2 branch as we should have.
Not caring enough to spell it full then :)
[snip]
To the OP, your solution (2) may work if you virtually inherit:
class fltk::Widget
class Fl_Widget : public virtual fltk::Widget
class fltk::Group : public virtual fltk::Widget
class Fl_Group : public virual fltk::Group, public virtual Fl_Widget
class fltk::Window : public virtual fltk::Group
class Fl_Window : public fltk::Window, public virtual Fl_Group
Thank you, I will try that out. Does that mean that the virtually
inherited classes must be pure virtual, too?
Not necessarily. That depends on own your class map to one another.
It solves the diamond shape inheritance. That way you will have the
following hierarchy:
Widget
| \
| FL_Widget
| |
Group |
| \ |
| FL_Group
| |
Window |
\ |
FL_Window
Group and FL_Group will share the same inherited Widget instance.
Francesco: (3) I see no reason why (3) wouldn't work, but I havn't
tested it yet. It feels a bit brute force to me and I was hoping for a
more elegant version.
If you don't need to bridge the two hierarchy (i.e. a FL_Widget* must
not be a fltk::Widget*), you could just use composition and keep the
types orthogonals:
// FL_Widget uses internally a fltk::Widget.
class FL_Widget
{
fltk::Widget* widget;
public:
FL_Widget():widget(new fltk::Widget())
{}
// interface uses widget
protected:
FL_Widget(fltk::Widget* w):widget(w)
{}
void setWidget(fltk::Widget* w)
{
widget=w;
}
};
And in case of inheritance, you intialize according to decorated
object:
class FL_Group: public FL_Widget
{
fltk::Group* group;
public:
FL_Group():FL_Widget(NULL)
,group(new fltk::Group())
{ setWidget(group); }
// interface uses group
protected:
FL_Group(fltk::Group* g):FL_Widget(g),group(g)
{}
void setGroup(fltk::Group* g)
{
group=g;
setWidget(group);
}
};
// ...
You would also have to redefine copy constructor and operator to make
your classes canonical; beware of destructor which must set its
hierarchy to NULL to avoid multiple deletion.
--
Michael