Re: G_CONST_RETURN



On Fri, 2004-11-19 at 12:56 -0700, Neil Zanella wrote:
> Hello,
> 
> When looking at the GTK API I see several places that use G_CONST_RETURN.
> What are these macros there? I cast their effect away with casts, but I am not
> sure whether that is the right thing to do. I wonder whether someone can
> elaborate on this (not sure why a function would have to return a
> const value any more).

Functions that return const values are hints to you that you should not
free the results. The best thing to do is to take advantage of this
and make the variables you assign the results to const.

If you do:

 const char *text = gtk_entry_get_text (entry);
 g_free (text); 

The compiler will catch the mistake. Which is what you want.

Because propagating const throughout a program can end up being a
cumbersome task, we use G_CONST_RETURN rather than const and provide
G_DISABLE_CONST_RETURNS. But unless you are adapting a large existing
code base, you should take advantage of the const rather than casting
it away or using G_DISABLE_CONST_RETURNS.

Regards,
						Owen

Attachment: signature.asc
Description: This is a digitally signed message part



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