While practicing a tutorial on GTK+ I have encountered sample code that looks like this:
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
all of the authors code has a space between function and (), but so does the typecasts. obviously gtk_misc_set_alignment() is a function, but how do I tell if GTK_MISC (label) is a function or a typecast?
Sorry for the noob question, I am a noob programmer, Thanks in advance
Actually, GTK_MISC
is a macro that hides a "classic" C typecast. It's probably something like:
#define GTK_MISC(p) ((GtkMisc *)(p))
You could instead simply write:
gtk_misc_set_alignment ((GtkMisc *) label, 0, 0);
I don't know exactly why GTK provides such macros, maybe they like to "emulate" the "function-like" cast that C++ provides.
Edit
Ok, maybe I got it. I didn't find a specific documentation for GTK_MISC
, but it seems to be exactly the same thing as G_OBJECT
, which says:
#define G_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), G_TYPE_OBJECT, GObject))
Casts a
GObject
or derived pointer into a(GObject*)
pointer. Depending on the current debugging level, this function may invoke certain runtime checks to identify invalid casts.
So, probably GTK_MISC
too performs some runtime checks on the pointer to check if it can be actually casted to a GtkMisk *
. You could say that it is somewhat the concept of dynamic_cast
in C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With