PDA

View Full Version : C Gtk and casting



cl333r
January 16th, 2009, 08:19 AM
Hi,
I'm using C and I noticed I have to cast the widgets to the exact type to avoid warnings. Is there a way to get rid of this?

i.e. I have to cast widgets "back and forward" depending on whether the methods expect a GtkWidget or GtkWindow. Afaik GtkWindow "extends" GtkWidget so i shouldn't have to cast it to GtkWidget, the method should be aware of that. Other languages do just that and it greatly simplifies my code.

eye208
January 16th, 2009, 11:06 AM
I'm using C and I noticed I have to cast the widgets to the exact type to avoid warnings. Is there a way to get rid of this?
You could turn off the warnings, but I wouldn't recommend it.


Afaik GtkWindow "extends" GtkWidget so i shouldn't have to cast it to GtkWidget, the method should be aware of that.
C does not support inheritance. The compiler has no way to tell if GtkWindow is a subclass of GtkWidget.


Other languages do just that and it greatly simplifies my code.
If you want to take full advantage of OOP, use C++ instead of C. The gtkmm library provides an OOP wrapper for GTK.

mike_g
January 16th, 2009, 12:07 PM
I really think you want to be casting, otherwise you are more likely to end up with a hideous mess.

Gtk comes with a set of macros to cast and assert widgets. Yes, its verbose, but I would recommend using them.

cl333r
January 16th, 2009, 01:44 PM
Thanks both,

C does not support inheritance.The compiler has no way to tell if GtkWindow is a subclass of GtkWidget.
I think that's my issue. If I wouldn't be coming from Java I prolly wouldn't have noticed that.