Re: g_slist and const



On Sat, 2007-03-10 at 11:25 +0100, Asbj�rote:
> On Saturday 10 March 2007 00:23, you wrote:
> > Asbj�rote:
> > > I use C++/glib and i get this  strange looking function:
> > >
> > > guint my_class::length (const GSList  *list)  /* const to protect my list
> > > */ {
> > >    /* cast it to a non const pointer! */
> > >     return (g_slist_length  ((GSList *) list));
> > > }
> > >
> > > So i want glib "C" to be more "C++" friendly    :)
> >
> > Just use Glibmm.
> >
> >
> > Hub
> 
> Here is my Glibmm version of my function :
> 
> guint my_class::length (const GSList  *list)  /* const to protect my list */
> {
>   /* still cast it to a non const pointer! */
>      class SListHandle  cclist ((GSList *) list));
>      return (cclist.size());  
>   /* and then the destructor will free the list !! eeek;  */
>  }  

glibmm's SListHandle is not meant to be used in this way. It's an
intermediate type for use in APIs, so that you can use the C++ container
of your choice, such as std::list:
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch03s05.html

(I'm not a big fan of that technique, because it requires you to read
documentation, but that's the decision we made a long time ago.)

It's not a good idea to try using them in your own code.

The C++ equivalents of GList and GSList are std::list, std::vector and
friends. We don't attempt to reimplement this functionality
unnecessarily. glib provides them because C lacks data structures. C++
does not.

If you really want to use GList, or any other C API, in your C++ code,
just get used to using const_cast<>.

> This will not work, so i have to take a copy of the list first, and then
> estimate the size of the copied list.  :)
> The Glibmm people have build and protected the GSList pointer inside their
> SListHandle class.  So the GSList pointer is an "global" variable inside the 
> class.  I don't like global variables :(

This doesn't make any sense. There are no global variables for
SListHandle. Please file a bug if you think you've found any.

[snip]

-- 
Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com




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