Re: Quick question on Glib style/design

On Tue, 2004-08-03 at 17:25 +0900, Ryan McDougall wrote:
> On Tue, 2004-03-08 at 10:06 +0200, Murray Cumming wrote:
> > Surely you will still have to cast to a gpointer (void*) just as much as
> > you would have to cast to A*, plus you'll have a totally obsure API. This
> > is C, and casting is part of the deal with gobject. Get a better
> > programming language if you want the language to do more work for you.
> > 
> > Something like gtk_widget_show(GtkWidget* widget) is an example of what
> > you want. People always need to cast derived widget pointers (such as
> > GtkTreeView*) to the base GtkWidget*, and people seem to have got used to
> > doing that.
> Actually the whole point of the email is that the compiler DOESN'T
> complain when I pass an X pointer to a void pointer. Is the compiler
> broken? Its GCC 3.3 on FC2.
> As I said previously, gpointer works without making the user do the
> casting. I'm not sure you read my email well, because I didn't say I
> wanted the language to do the work for me, I said I wanted ME to do the
> work for the user by doing the cast inside the function.

I think Murray is pointing out that accepting a void pointer and then
casting inside your function is stylistically different to most other
GObject based applications.

I would go with:

- Create an object Foo using GObject (and all the convenience macros)

- Create an object Bar that inherits from FOO, again using GObject

- You can then define methods/functions such as:

	foo_do_action (Foo *obj);
	bar_do_action (Bar *obj);

- Inside foo_do_action you would check:

	g_assert (FOO_IS (obj));

- You can use these:

	Bar *example;

	foo_do_action (FOO (example));

>From my experience, this is how almost all Gnome libraries work, so
programmers are very familiar with this style.


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