Re: What's problem with my source code?
- From: Tristan Van Berkom <vantr touchtunes com>
- To: Ëιúΰ <gwsong_jl sohu com>
- Cc: "gtk-app-devel-list gnome org" <gtk-app-devel-list gnome org>
- Subject: Re: What's problem with my source code?
- Date: Mon, 28 Oct 2002 10:19:58 -0500
try putting:
parent_class = g_type_class_peek_parent(g_class)
in your class_init of person. (This will initialize
the GObject parent and give you the correct address if
you wish to use polymorphism).
also:
Peter and Tom are people and GObject's
(If initialized properly). Peter and Tom are
not TestIFaces.
-Tristan
Ëιúΰ wrote:
Hi, everyone!
I write a program to test GTypeInterface .
The code here:
//----------------------------------------------
//test.c
#include <glib.h>
#include <glib-object.h>
#define TYPE_TEST_IFACE (test_iface_get_type())
#define TEST_IFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),TYPE_TEST_IFACE,TestIFace))
#define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj),TYPE_TEST_IFACE,TestIFaceClass))
typedef struct _TestIFace TestIFace;
typedef struct _TestIFaceClass TestIFaceClass;
struct _TestIFaceClass
{
GTypeInterface base;
//
void (*output) (gchar* string);
void (*hello) (void);
};
GType test_iface_get_type(void);
static gint count=0;
static void test_iface_init(TestIFaceClass* klass);
static void test_iface_finalize(TestIFaceClass* klass);
GType test_iface_get_type(void)
{
static GType test_iface_type = 0;
if(!test_iface_type)
{
static const GTypeInfo test_iface_info = {
sizeof(TestIFaceClass),
(GBaseInitFunc)test_iface_init,
(GBaseFinalizeFunc)test_iface_finalize,
NULL,NULL,NULL,0,0,NULL
};
test_iface_type = g_type_register_static(G_TYPE_INTERFACE,"TestIFace",&test_iface_info,0);
g_type_interface_add_prerequisite(test_iface_type,G_TYPE_OBJECT);
}
return test_iface_type;
}
static void test_iface_init(TestIFaceClass* klass)
{
count++;
}
static void test_iface_finalize(TestIFaceClass* klass)
{
count--;
}
// the person object
#define TYPE_PERSON (person_get_type())
#define PERSON(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),TYPE_PERSON,Person))
#define PERSON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj),TYPE_PERSON,PersonClass))
typedef struct _Person Person;
typedef struct _PersonClass PersonClass;
struct _Person {
GObject parent_instance;
//
gint age;
gchar* name;
};
struct _PersonClass {
GObjectClass parent_class;
};
GType person_get_type(void);
GObject* person_new(void);
void person_set_name(GObject* person, gchar* name);
gchar* person_get_name(GObject* person);
void person_set_age(GObject* person, gint age);
gint person_get_age(GObject* person);
GObject* person_new_with_name_and_age(gchar* name, gint age);
void person_output(GObject* person);
void iface_print(TestIFace* iface, gchar* string);
void iface_hello(TestIFace* iface);
//
static void output(gchar* string);
static void hello(void);
static void iface_init(gpointer iface, gpointer data);
static void person_init(Person *person);
static void person_class_init(PersonClass *klass);
GType person_get_type(void)
{
static GType person_type = 0 ;
if(!person_type)
{
static const GTypeInfo person_info = {
sizeof(PersonClass),
NULL,
NULL,
(GClassInitFunc)person_class_init,
NULL,
NULL,
sizeof(Person),
0,
(GInstanceInitFunc)person_init
};
static const GInterfaceInfo iface_info = {
(GInterfaceInitFunc)iface_init,
NULL,NULL};
person_type = g_type_register_static(G_TYPE_OBJECT,"Person",&person_info,0);
g_type_add_interface_static(person_type,TYPE_TEST_IFACE,&iface_info);
}
return person_type;
}
static void person_init(Person *person)
{
person->age = 0;
person->name = NULL;
}
static void person_class_init(PersonClass *klass)
{
//without code here
}
GObject* person_new(void)
{
return g_object_new(TYPE_PERSON,NULL);
}
void person_set_name(GObject* person, gchar* name)
{
PERSON(person)->name = name;
}
gchar* person_get_name(GObject* person)
{
return PERSON(person)->name;
}
void person_set_age(GObject* person, gint age)
{
PERSON(person)->age = age;
}
gint person_get_age(GObject* person)
{
return PERSON(person)->age;
}
GObject* person_new_with_name_and_age(gchar* name, gint age)
{
GObject* person;
person = person_new();
person_set_name(person, name);
person_set_age(person, age);
return person;
}
void person_output(GObject* person)
{
g_print("This person name is %s, age is %d.\n",
person_get_name(person), person_get_age(person));
}
static void iface_init(gpointer iface, gpointer data)
{
TestIFaceClass *klass = iface;
g_assert(G_TYPE_FROM_INTERFACE(iface) == TYPE_TEST_IFACE);
klass->output = output;
klass->hello = hello;
}
void output(gchar* string)
{
g_print("The string is : %s \n", string);
}
void iface_print(TestIFace* iface, gchar* string)
{
TestIFaceClass* klass;
klass = TEST_IFACE_GET_CLASS(iface);
klass->output(string);
//TEST_IFACE_GET_CLASS(iface)->output(string);
}
void iface_hello(TestIFace* iface)
{
TEST_IFACE_GET_CLASS(iface)->hello();
}
void hello(void)
{
g_print("Hello everyone!\n");
}
// the main function
int main(int argc, char *argv[])
{
GObject *tom, *peter;
//Person* person;
g_type_init();
tom = person_new();
person_set_name(tom, "Tom London");
person_set_age(tom, 18);
//
person_output(tom);
peter = person_new_with_name_and_age("Peter White",20);
//
person_output(peter);
//iface_print(TEST_IFACE(peter),"this is peter");
iface_print(TEST_IFACE(peter),"this is peter");
iface_hello(TEST_IFACE(tom));
//person = g_object_new(TYPE_PERSON,NULL);
//person_output(G_OBJECT(person));
//iface_hello(TEST_IFACE(person));
}
//------------------------------------------------------
the Makefile here :
CC = gcc
all:
$(CC) -o t test.c `pkg-config --libs --cflags glib-2.0 gobject-2.0`
//------------------------------------------------------
When I run ./t it's output message:
This person name is Tom London, age is 18.
This person name is Peter White, age is 20.
(process:647): GLib-GObject-WARNING **: cannot create new instance of invalid (non-instantiatable) type
`(null)'
Segmentation fault
//------------------------------------------------------
What's problem with my program?
thanks everyone!
gwsong_jl sohu com
,Ù¦-^½éeSËfj)b? b²Ø-?ªiuëÞ-X¬¶ è?ê+,m§ÿæj)`???¢¸??¨¥?©ÿ-+-Swèþdjs]z÷¥-+-
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]