三 可实例化的类型:对象(Instantiable classed types: objects)
来源:互联网 发布:凯立德v4.0端口修改器 编辑:程序博客网 时间:2024/06/14 20:24
可实例化的类型:对象(Instantiable classed types: objects)
对于那些注册为类并且声明为实例的类型是最象对象的类了。(用中文太难表达了)。
Instantiable classed types: objects
Types which are registered with a class and are declared instantiable are what most closely resembles an object. GObjects are the most well known type of instantiable classed types. Wo will discuss GObject later, here we wil discuss some other instantiable classed types, which used as the base of an inheritance hierarchy have been externally developped and they are all built on the fundamental features described below.
For example, the code below shows how you could register such a fundamental object type in the type system:
Upon the first call to
Every object must define two structures: its class structure and its instance structure. All class structures must contain as first member a GTypeClass structure. All instance structures must contain as first member a GTypeInstance structure. The declaration of these C types, coming from
These constraints allow the type system to make sure that every object instance (identified by a pointer to the object's instance structure) contains in its first bytes a pointer to the object's class structure.The C standard mandates that the first field of a C structure is stored starting in the first byte of the buffer used to hold the structure's fields in memory. This means that the first field of an instance of an object B is A's first field which in turn is GTypeInstance's first field which in turn is g_class, a pointer to B's class structure.
(to be continue....)
对于那些注册为类并且声明为实例的类型是最象对象的类了。(用中文太难表达了)。
Instantiable classed types: objects
Types which are registered with a class and are declared instantiable are what most closely resembles an object. GObjects are the most well known type of instantiable classed types. Wo will discuss GObject later, here we wil discuss some other instantiable classed types, which used as the base of an inheritance hierarchy have been externally developped and they are all built on the fundamental features described below.
For example, the code below shows how you could register such a fundamental object type in the type system:
typedef struct ...{
GObject parent;
/**//* instance members */
int field_a;
} MamanBar;
typedef struct ...{
GObjectClass class_parent;
/**//* class members */
void (*do_action_public_virtual) (MamanBar *self, guint8 i);
void (*do_action_public_pure_virtual) (MamanBar *self, guint8 i);
} MamanBarClass;
#define MAMAN_BAR_TYPE (maman_bar_get_type ())
GType
maman_bar_get_type (void)
...{
static GType type = 0;
if (type == 0) ...{
static const GTypeInfo info = ...{
sizeof (MamanBarClass),
NULL, /**//* base_init */
NULL, /**//* base_finalize */
(GClassInitFunc) foo_class_init,
NULL, /**//* class_finalize */
NULL, /**//* class_data */
sizeof (MamanBar),
0, /**//* n_preallocs */
(GInstanceInitFunc) NULL /**//* instance_init */
};
type = g_type_register_static (G_TYPE_OBJECT,
"BarType",
&info, 0);
}
return type;
}
GObject parent;
/**//* instance members */
int field_a;
} MamanBar;
typedef struct ...{
GObjectClass class_parent;
/**//* class members */
void (*do_action_public_virtual) (MamanBar *self, guint8 i);
void (*do_action_public_pure_virtual) (MamanBar *self, guint8 i);
} MamanBarClass;
#define MAMAN_BAR_TYPE (maman_bar_get_type ())
GType
maman_bar_get_type (void)
...{
static GType type = 0;
if (type == 0) ...{
static const GTypeInfo info = ...{
sizeof (MamanBarClass),
NULL, /**//* base_init */
NULL, /**//* base_finalize */
(GClassInitFunc) foo_class_init,
NULL, /**//* class_finalize */
NULL, /**//* class_data */
sizeof (MamanBar),
0, /**//* n_preallocs */
(GInstanceInitFunc) NULL /**//* instance_init */
};
type = g_type_register_static (G_TYPE_OBJECT,
"BarType",
&info, 0);
}
return type;
}
Upon the first call to
maman_bar_get_type
, the type named BarType will be registered in the type system as inheriting from the type G_TYPE_OBJECT.Every object must define two structures: its class structure and its instance structure. All class structures must contain as first member a GTypeClass structure. All instance structures must contain as first member a GTypeInstance structure. The declaration of these C types, coming from
gtype.h
is shown below:struct _GTypeClass
{
GType g_type;
};
struct _GTypeInstance
{
GTypeClass *g_class;
};
{
GType g_type;
};
struct _GTypeInstance
{
GTypeClass *g_class;
};
This relationship is best explained by an example: let's take object B which inherits from object A:
/* A definitions */
typedef struct {
GTypeInstance parent;
int field_a;
int field_b;
} A;
typedef struct {
GTypeClass parent_class;
void (*method_a) (void);
void (*method_b) (void);
} AClass;
/* B definitions. */
typedef struct {
A parent;
int field_c;
int field_d;
} B;
typedef struct {
AClass parent_class;
void (*method_c) (void);
void (*method_d) (void);
} BClass;
typedef struct {
GTypeInstance parent;
int field_a;
int field_b;
} A;
typedef struct {
GTypeClass parent_class;
void (*method_a) (void);
void (*method_b) (void);
} AClass;
/* B definitions. */
typedef struct {
A parent;
int field_c;
int field_d;
} B;
typedef struct {
AClass parent_class;
void (*method_c) (void);
void (*method_d) (void);
} BClass;
(to be continue....)
- 三 可实例化的类型:对象(Instantiable classed types: objects)
- 二 Non-Instantiable non-classed fundamental types
- Git对象类型(Object types)
- GObject参考手册(6)--可实例化的类型:对象
- 130.Oracle数据库SQL开发之 数据库对象——NOT INSTANTIABLE对象类型
- .Net 2.0 新功能:可空类型(Nullbale Types)
- Form Builder开发中可重用对象(Resuing Objects)
- Spring笔记之七(Types of Injection) Spring的三种依赖注入实现类型
- Primitive Types and Objects
- Sybase objects/types
- Python Types and Objects
- Python Types and Objects
- Unity3d游戏开发中使用可空类型(Nullable Types)
- 不完全类型(Partial Types)
- QML类型(QML Types)
- 函数对象(Function Objects)
- 函数对象(Function Objects)
- 不变对象(Immutable Objects)
- linux设备驱动程序第二版 中断处理
- Javascript中最常用的55个经典技巧
- vbsU盘传播病毒
- [ztjSQL]索引
- linux设备驱动程序第二版 合理使用数据类型
- 三 可实例化的类型:对象(Instantiable classed types: objects)
- dojo0.9的第一个实例
- sanshi0815开启seo优化之门
- linux设备驱动程序第二版 kerneld和高级模块化
- linux设备驱动程序第二版 加载快设备驱动程序
- 这才叫爱女朋友
- 一段文本是如何统计汉字的?
- linux设备驱动程序第二版 MMAP和DMA
- 我喜欢安静的学习环境!Show一下我的学习角落:)