GObject - Virtual Function

svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only


= Member Function =
: the functions which have a self parameter

If you ever write python code to implement about CLASS. The class member function may look like bellow:

def f(self):
return 'hello world'

You can find out the first parameter always be class itself. And the function will use 'self' to access the member value. The same principle we use in GObject programming.
We always use thiz (instead this, because C++ syntax) for first parameter in each member functions. Whether public function or private function.

= Public Member Function =
: What's different between "Just Function" and "Public Member Function"?

Same as C++, in GObject programming, all the functions define in the class structure header with first self parameter are public member functions.
In example 5-1 we use a public member function to replace what we done in main.c, dump all member value.

void maman_bar_dump_all_value( MamanBar* thiz )
printf(" bar->a = (%d) bar->b = (%d)\n", thiz->a, thiz->b );

= Private Member Function =
: just simple rule, static functions (with first self parameter) are private

It's too simple. I believe we don't need to explain this idea.

= Member Function with Virtual Feature =

/* 5-2 */
/* maman-bar.h */
struct _MamanBarClass {
GObjectClass parent;
/* class members */

void (* incAll) ( MamanBar* );

/* maman-bar.c */
static void maman_bar_class_init ( MamanBarClass* class)
class->incAll = maman_bar_inc_all;

In this step, we need to explain what's different between MamanBar and MamanBarClass. We use g_object_new to allocate a memory size from heap, and g_object_new invoke function maman_bar_init to initialize this memory region. But different from MamanBar, MamanBarClass should only exists one copy during one time period.

maman_bar_dump_all_value( bar );
MAMAN_BAR_GET_CLASS( bar ) -> incAll(bar);
maman_bar_dump_all_value( bar );

In above sample code, you can see we use MAMAN_BAR_GET_CLASS to access CLASS structure, and use it to invoke the class member functions.
The whole idea in virtual function is child class will use it's member function replace the parent class's member function pointer.

= Virtual Function =

Base on example 4-2. We implement sub-bar class and replace the parent's class function pointer.

/* 5-3 */
/* sub-bar.c */
static void sub_bar_class_init ( SubBarClass* class)
MamanBarClass* parent_class = class;
parent_class->incAll = sub_bar_inc_all;

After constructor invoke (base_init and class_init ...), we can invoke child function by using parent's fp. It's simple and easy to use.

/* main.c */
SubBar *subbar = g_object_new (SUB_BAR_TYPE, NULL);
sub_bar_dump_all_value( subbar );
MAMAN_BAR_GET_CLASS( subbar ) -> incAll( subbar );
sub_bar_dump_all_value( subbar );

1 則留言:

  1. Merit Casino Review 2021 | Pros & Cons, Games & Bonuses
    Read our Merit Casino review for 메리트카지노 the company's members to see what you think about it 더킹카지노 and see what they think 메리트카지노 it has to offer.