12. Step 1 :定義物件
● mamanbar.h
typedef struct _MamanBar MamanBar;
typedef struct _MamanBarClass MamanBarClass;
struct _MamanBar { 繼承 GObject
GObject parent;
/* instance members */
int a;
int b;
};
struct _MamanBarClass {
GObjectClass parent;
/* class members */
};
elpam.tw@gmail.com
13. 將 function 與 value 分開
●
在 GObject 的設計裡,與 C++ 最大的概念上的
不同就是 member function 與 member value
擺放的位置
– maman_bar 放 member value
– maman_bar_class 放 member function
● Why?
– member value 是每一個物件都要有一份
– member function 則是每一個物件都使用同一份
elpam.tw@gmail.com
14. 精確的說
●
class 裡頭放的資料是所有的物件都存取同一份
– C++ 的 static member value
– 是的, class 裡不一定要放 function point
– 不是 static member function
● 而 base 裡頭放則是各個物件都保存一份
elpam.tw@gmail.com
15. Step 2 :註冊 GType
● /* mamanbar.c */
請注意:此 Function 為 Singleton ,而此函式只
GType maman_bar_get_type (void) 會 if(){} 裡只會被執行一次
{
static GType type = 0;
if (type == 0) { 將自已所撰寫的 function 填寫於此
static const GTypeInfo info = {
/* You fill this structure. */
};
type = g_type_register_static (G_TYPE_OBJECT,
"MamanBarType",
&info, 0);
}
return type;
}
elpam.tw@gmail.com
22. Step 4 : use it
● Add print out information in the maman_bar_init
● Please monitor the order of function launched
int main (int argc, char *argv[])
{
/* this function should be executed first. Before everything */
g_type_init();
You can find out the constructor/ type register
will be launched by this function
/* Create our object */
MamanBar *bar = g_object_new (MAMAN_TYPE_BAR, NULL);
printf(" bar>a = (%d) bar>b = (%d)n", bar>a, bar>b );
return 0;
}
elpam.tw@gmail.com
23. GObject 42
Inherit
Step By Step
elpam.tw@gmail.com
24. Step 1 : Define SubBar
● Please Notice This Color /* subbar.h */
typedef struct _MamanBar MamanBar;
typedef struct _MamanBarClass MamanBarClass;
struct _SubBar { 繼承 GObject
MamanBar parent;
/* instance members */
int c;
int d;
};
elpam.tw@gmail.com
26. Step 3 : Try Casting
● /* main.c */
● When we need to access parent's member
value
((MamanBar*)subbar)>a, ((MamanBar*)subbar)>b
elpam.tw@gmail.com
27. Step 4 : ....
● Yes, it's very simple. No trick like previous slide
● Please monitor the order of function launched
elpam.tw@gmail.com
28. Conclusion
● The above example show the inheritance
implementation by using Gobject.
● Reader can write to child inherit subbar, and
monitor the timing of constructor.
elpam.tw@gmail.com
29. Next
● We''ll talk about Virtual Function later
elpam.tw@gmail.com