So i have been doing some work with GTK+ and i have noticed they they tend to build their system in such a way that supports a sort Pseudo Polymorphism. I'm a huge fan of this but I'm not quite grasping how they do their type casting?
[CODE]
GtkWindow *window = GTK_WINDOW (buildable);
[/CODE]
So im still doing some digging but figured i would ask my favorite people at Facepunch first.
The code looks to my like they are Casting some var buildable (of what type I'm not quite sure) to the type GtkWindow. My best guess is that buildable is a "super class" of the GtkWindow but again I'm probably wrong, i have seen some examples online of different ways of implementing OOP in C but none of them seem to match this style. Maybe I'm just missing something but who knows, i guess im going to keep doing some digging through the source and get to the bottom of this! But if anyone with experience with the GTK+ framework or even a slight idea on how they go about doing this could point me in the right direction i would forever be in debt to you.
*edit*
So it would seem that after exploring the header files for each of the "classes" they have been using Preprocessor definitions to shield me from the pain of the typecasting by hand, some of the code is starting to look like other examples i have found on the internet. Still any advice on this topic would be awesome! i guess i need to brush up more on my Preprocessor for C.
GTK_WINDOW looks like a function (in use) to me, it could just be returning an address to a window object/struct/whatever it may be.
If you looked into GTK_WINDOW, it might be a fairly complex blob of pre-processor defines as a wrapper for a more complex blob of code and/or functions.
C based polymorphism is... difficult to keep track off in my experience due to the inheritance dangers of pointer casting, so I tend to not use it too much if I can reasonably avoid it; C++ does it much better IMO.
The OOP system used by GTK+ is called GObject. It's a separate library with [url=https://developer.gnome.org/gobject/stable/]its own documentation[/url]. The [url=https://developer.gnome.org/gobject/stable/gtype-conventions.html]conventions page[/url] in the documentation describes the various type-checking and casting macros that are typically associated with each GObject type.
A casting macro like GTK_WINDOW is based on the G_TYPE_CHECK_INSTANCE_CAST macro that actually does the work. It performs a run-time check that the argument is actually an instance of the expected type, and prints a warning to stderr if it isn't.
Theoretically, one might implement pseudo-polymorphism using variable argument lists
void* everywhere
Sorry, you need to Log In to post a reply to this thread.