Re: Virtual functions and chaining up
- From: David Nečas (Yeti) <yeti physics muni cz>
- To: gtk-app-devel-list gnome org
- Subject: Re: Virtual functions and chaining up
- Date: Thu, 27 Jul 2006 09:39:08 +0200
On Thu, Jul 27, 2006 at 02:24:23AM -0400, Nikhil Dinesh wrote:
I'm trying to create my own object hierarchy, and I don't
understand the part in the API reference about virtual
functions and chaining up. To create a virtual public
method, the docs say we need to do something like:
/* declaration in maman-bar.h. */
struct _MamanBarClass {
GObjectClass parent;
/* stuff */
void (*do_action) (MamanBar *self, /* parameters */);
};
void maman_bar_do_action (MamanBar *self, /* parameters */);
/* implementation in maman-bar.c */
void maman_bar_do_action (MamanBar *self, /* parameters */)
{
MAMAN_BAR_GET_CLASS (self)->do_action (self, /* parameters */);
}
Correct.
I assume that the subclass definition looks like:
struct _MamanBarSubClass {
MamanBarClass parent;
....
void (*do_action) (MamanBarSub* self, /* params */);
}
No, there's already one do_action() in parent, so this is
a second one. Moreover the parent class has no access to
this second do_action() so maman_bar_do_action() can never
invoke it.
Each class has its own class struct which contains all
ancestors's class structs (like a matryoshka). When its
class_init() method is called, the fields defined in
ancestors classes are already filled with parent's values.
So you only need to override them.
If the maman_bar_do_action actually needs to do something, and
the derived class needs to chain up,
maman_bar_do_action() is *not* the parent class method, it's
the virtual method for users to call. The child method
calls its parent's method:
MAMAN_BAR_CLASS(maman_bar_sub_parent_class)->do_action().
See Gtk+ source code for examples...
Yeti
--
Anonyms eat their boogers.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]