求教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;
}
其中的两句让我理解不能,还请这里的前辈能够指点一二这两句是
………………
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28