Re: Quick question on Glib style/design




On Aug 3, 2004, at 1:43 AM, Ryan McDougall wrote:

I want MY functions to do the casting (which is unavoidable anyways), but the dang compiler won't stop complaining!

won't stop complaining about what? in C, anything is valid where a void* is wanted, so the compiler will accept this without complaint. (this is not true in C++, IIRC.)


Is this a good idea? Clearly its not extensible to more involved hierarchies since I'll have nothing but gpointers.

no, it's not a good idea. you've saved the programmer from needing to cast something, but now you've made your API more obscure by hiding what type your function really wants, and placed *more* burden on the programmer, who now needs intimate knowledge of how your API works.

if what you want is really to save the programmer from casting, then in C you'll need to use macros, e.g.:

    /* don't try this at home */
    void foo__bar (Foo * f);
    #define foo_bar(f)  foo__bar((Foo*)(f))

but this is also, in general, Not A Good Thing as you have totally sidestepped the compiler's type checking. for example, image somebody tries to pass a Flurble* to foo_bar() --- it will take quite a bit of swimming through headers to find that it's the wrong type (even with the void* declaration you described above), because the compiler will not complain at all about the bad type.

if you have

   void foo_bar (Foo * f);

and you want to pass a Bar* to it, you are instantly reminded that the Bar needs to derive from a Foo. this is idiomatic to C and especially to GLib.


--
"Ghostbusters" is the best movie of this decade.
  -- Neal, circa 1996, referring to a movie released in 1984.




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