Re: New 'GObject' as base for GtkObject?

Owen Taylor <> writes:

> > (separate idea) How do you export specific pieces of the
> > interface?  For example, a frame has 2 container interfaces.  I
> > want to pass one of them to something that is going to add a
> > dialog in.  How would I code that?  How would that reference so
> > that it could call signals?
> I don't see this as inheritance - the classic dictum is that
> inheritance models is_a relationships, not has_a
> relationships. [...]  The right way to do this is with aggregation
> or helper objects (more below)

You're absolutely right.

> I've been working some with Python recently, and in
> a language like Python that has first class function objects,
> the way to do this is obvious:
>  def place_tic_tack_toe (self, func):
>     func(self.thing_to_place)
>  place_tic_tack_toe(paned.add1)
> That just works right now. Almost like cheating, isn't it? I'd do the
> same thing in Scheme, and maybe even Perl, thoughj neither is as
> compact. Unfortunately, function pointers in C or C++ aren't as
> powerful, or as convenient, but you could pass in a function / closure
> data pair, or you could use helper objects.

You can have function objects in C++. Yes, it's yet another idiom (and
it can look really ugly), but it's widely used, extremely well known
and fairly close to the original idea.

> Helper objects is probably the way I'd do things in C++ or Java
> ... something like:

You might like this better :


class Gtk_AddInPaned : public Placer // this will be your function object
  Gtk_AddInPaned(Gtk_Paned &paned, int side) : mPaned(paned), mSide(side) {};
  void operator()(Gtk_Widget &child);

  Gtk_Paned &mPaned;
  int mSide;

Gtk_AddInPaned::operator()(Gtk_Widget &child)
  if (mSide == 1)
    mPaned.add1 (child);
    mPaned.add2 (child);

/// and in your TicTackToe class, you have :

TicTackToe::place(Placer &placer)

/////////// and this can be used as follows :

Gtk_Paned myPaned;
TicTackToe myTickTackToe;, 1));

Who said operator overloading was evil ? :-)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]