> > class Control {
> > public:
> >     virtual void set_value(int) = 0;
> >     virtual int get_value() const = 0;
> > };
> > class Slider : public Fl_Slider, public Control {
> > public:
> >     Slider(int x, int y, int w, int h, char const* label = 0)
> >         : Fl_Slider(x, y, w, h, label)
> >     {;}
> >     void set_value(int v) { Fl_Slider::value(v); }
> >     int get_value() const { return Fl_Slider::value(); }
> > };
> >
> > // similarly for other classes of interest
> >

> I am wondering if you meant Slider* when you said "Then, 
> store a container of Control*,...". Container would be an 
> array right? Do you mean i should have two containers, one 
> for the Control* and one for the Slider*? I think I am coming 
> closer to understand this approach. I just have no idea what 
> i should do with an container of Control*. :)

No, Stan meant that you should make an array of Control*

What he is doing here (which is probably not obvious to a C programmer!)
is making a class "Slider" that derives from both "Fl_Slider" *and* from
his new "Control" class - so, in principle, you can point to a "Slider"
widget using any of "Slider", "Control" or "Fl_Slider" as the pointer
type.

Now, in his "Control" class, he's declared all the methods as virtual,
so if you call any of those methods, the C++ runtime will search for the
equivalent method in the *actual* class and execute that.

So what you do is create a Slider class based on Fl_Slider and Control.
A Dial class based on Fl_Dial and Control.
A Button class based on Fl_Button and Control...
Etc...

Then, you make an array of Control* to hold them all, but when you call

(Control*)->set_value(...);

The code will seek out the *actual* class of the widget and call it's
set_value method.
Which is probably what you want to do.

By extension, you can probably put a widget ID mechanism in via the
Control base class to track your widgets, if you need that over and
above the array index.

An alternative approach is to give each class (Slider, Dial, etc.) a
method (e.g. int get_my_class(void)) that returns a unique value, and
use that to identify each Control* underlying class at runtime. (Note:
this is a budget version of RTTI, if you can be bothered googling it...)
(Or, indeed, RTTI might work, too - although fltk has always been wary
of that - it didn't really work in a lot of early C++ compilers...)






SELEX Sensors and Airborne Systems Limited
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to