Re: [gtk-list] problems for C++( use gtk1.2.6)



> This is a multi-part message in MIME format.
> 
> ------=_NextPart_000_0048_01BF6478.B85B2F60
> Content-Type: text/plain;
> 	charset="big5"
> Content-Transfer-Encoding: quoted-printable
> 
> Hi. I have some problem with my codes.
> Because I want to use C++ in GTK+, not gtk--. But I have a little =
> trouble.=20

Not quite sure why you want to avoid gtk--, but I can tell you what 
your problem is.  

Methods pointers are not the same as function pointers.  They
aren't even the same size.  sizeof(void (*A::)())!=sizeof(void (*)())
Thus if you want to do gtk+ in C++ you can't use virtuals
nor methods to interact with gtk+ callbacks.  

Now to show the trouble spots...
[...]
> /* main_window.h */
[...]
>       void test2();
>       //callback function;
>       void do_exit( GtkWidget * widget , GtkWidget * window );
        ^^^^^^^^^^^^^
This function must be static.  Static makes methods the same as
functions on most compilers.  (Note, strictly speeking static
and C functions are not the same, but it works on virtually all
compilers, at least enough that gtk-- uses it extensively.)

>       void create_main_frame();
> };


> /*main_window.cc */
[...]
> void main_window::create_main_frame()
> {
>      void (main_window::*exit)( GtkWidget * widget , GtkWidget * window =
> ) =3D &main_window::do_exit;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Don't do this!

>      struct=20
>      {
>         char * label;
>         void (main_window::*func)();
[...]
>      //create a new button about close window;
>      close_button =3D gtk_button_new_with_label( "Close" );
>      gtk_signal_connect( GTK_OBJECT(close_button) , "clicked" , =
> GTK_SIGNAL_FUNC(&exit) , window );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      gtk_signal_connect( GTK_OBJECT(close_button) , "clicked" , =
 GTK_SIGNAL_FUNC(&main_window::exit) , window );


>      gtk_box_pack_start( GTK_BOX(down_box) , close_button , TRUE , TRUE =
> , 0 );
>      GTK_WIDGET_SET_FLAGS( close_button , GTK_CAN_DEFAULT );
[...]

> //...........................
> my codes can be compile(didn't have any error). But, if executing the  =
> program, it'll crash and close. And tell me this:
>     Illegal instruction ( core dumped)
> Please help me. Thanks a lot.
>                     Cooper.

Now this may not be the only problem, but likely part of it.  
Note, that with statics you can't use the this pointer at all,
thus C++ method bome hard to use.  You will end up having to
pass arround your class as the callback to most functions and
cast it to then call a method.  

Hope it helps.

--Karl
  Gtk-- Lead Developer



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