+ -
当前位置:首页 → 问答吧 → 求教GLIB编程时遇到的初级问题

求教GLIB编程时遇到的初级问题

时间:2010-07-23

来源:互联网

笨人初学GTK编程,在阅读GTK+/GNOME一书时,在第二章Glib里遇到一个介绍Glib里表操作的例程list.c,
其中的两句让我理解不能,还请这里的前辈能够指点一二这两句是
………………
nameList=g_list_sort(nameList,(GCompareFunc)ListItemComparisonFunction);
………………
g_list_foreach(nameList,(GFunc)ItemPrinter,NULL);
………………
这两句中的(GCompareFunc)、(GFunc)是什么作用?是强制类型转换的作用吗?
glib.h里定义了这两个类型吗?
而且我试着去掉(GCompareFunc)、(GFunc)后此程序还是能够顺利编译并执行的
难道这两个东西是摆设吗???

源程序如下
list.c
#include <glib.h>

void PrintTheList(gchar *listTitle,GList *theList)
{
    g_print("Here's how the list looks after %s \n\n",listTitle);
    theList=g_list_first(theList);
    while(theList!=NULL)
    {
         g_print("%s\n",(gchar *)theList->data);
         theList=g_list_next(theList);
     }
     g_print("\n\n\n");
}

gint ListItemComparisonFunction(gconstpointer a,gconstpointer b)
{
     return(g_strcasecmp((const gchar *)a,(const gchar *)b));
}

void ItemPrinter(gpointer listData,gpointer userData)
{
     g_print("%s\n",(gchar *)listData);
}

gint main(gint argc,gchar * argv[])
{
     gchar rodent1[]={"Drag,the gerbil"};
     gchar rodent2[]={"Drop,Drag's buddy"};
     GList *nameList=NULL;

     nameList=g_list_append(nameList,"Peter, shaper of code");
     nameList=g_list_append(nameList,"Heather,code-shaper's wife");
     nameList=g_list_append(nameList,rodent1);
     nameList=g_list_append(nameList,roden2);
     PrintTheList("initially adding some items",nameList);

     nameList=g_list_prepend(nameList,"Loudy ,programmer tamer");
     nameList=g_list_insert(nameList,"John,writer of checks");
     PrinstList("prepending and inserting some items",nameList);

     nameList=g_list_sort(nameList,(GCompareFunc)ListItemComparisonFunction);
     PrintTheList("sorting the list:,nameList);
   
     g_print("Printing the list now,using g_list_foreach()\n\n);
     g_list_foreach(nameList,(GFunc)ItemPrinter,NULL);
     g_print("\n\n\n");
   
     nameList=g_list_remove(nameList,rodent1);
     nameList=g_list_remove(nameList,rodent2);
     PrintTheList("removing the rodents",nameList);

     g_list_free(nameList);
     g_print("Hey look - now the list is in the order of importance too. \n);

     return 0;
}

作者: jacobzz   发布时间: 2010-07-23

这两个应该是函数指针类型。
去掉还可以正常编译说明函数类型是兼容的,当然不兼容也肯定不能用了。

作者: davelv   发布时间: 2010-07-23