Re: C++-classes.



On Fri, 2002-10-18 at 09:23, David Larsson wrote:
Hi.

How do you get this to work.

/* Class Definition */
class GtkClass
{
  public:
    void GtkClass();
    void Test();
  private:
    GtkWidget *button;
};

GtkClass::GtkClass()
{
  button=gtk_button_new();

  /* The part that doesn't work. */
  gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(Test), NULL);
}

How is the Method Test to be declared, and how do you call it from a gtk_signal_connect()?

Thanks

// David

You can't pass a method in as a callback function pointer. There is a
hidden parameter, this, to all methods which points to the instance of
the class.

Instead try:
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(clicked_cb), this);


where clicked_cb is:
void clicked_cb(GtkButton *button, GtkClass *c)
{
        c->Test();
}


That is the simplest way to get a callback calling a method and how I
have done it in my own projects.

James


-- 
James Durie               Phone:  +44 20 7749 7904
Anvil Software Limited    Fax:    +44 20 7749 7916
46-48 Rivington Street    e-mail: jdurie anvil co uk
London EC2A 3QP




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