Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to tell a typecast from a function

Tags:

c

gtk

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

like image 274
MrDetail Avatar asked Dec 02 '22 23:12

MrDetail


1 Answers

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++.

like image 87
Matteo Italia Avatar answered Dec 16 '22 13:12

Matteo Italia