Just as with C++, there are many different ways to define object
methods and extend them: the following list and sections draw on
C++ vocabulary. (Readers are expected to know basic C++ concepts.
Those who have not had to write C++ code recently can refer to e.g.
http://www.cplusplus.com/doc/tutorial/ to refresh
their memories.)
Non-virtual public methods
These are the simplest, providing a simple method which
acts on the object. Provide a function
prototype in the header and an implementation of that prototype
in the source file.
This is the preferred way to create GObjects with overridable methods:
Define the common method and its virtual function in the
class structure in the public header
Define the common method in the header file and implement it in the
source file
Implement a base version of the virtual function in the source
file and initialize the virtual function pointer to this
implementation in the object’s class_init
function; or leave it as NULL for a ‘pure
virtual’ method which must be overridden by derived classes
Re-implement the virtual function in each derived class which needs
to override it
Note that virtual functions can only be defined if the class is
derivable, declared using
G_DECLARE_DERIVABLE_TYPE
so the class structure can be defined.
The code above simply redirects the open call
to the relevant virtual function.
It is possible to provide a default
implementation for this class method in the object's
class_init function: initialize the
klass->open field to a pointer to the
actual implementation.
By default, class methods that are not inherited are initialized to
NULL, and thus are to be considered "pure virtual".
These are very similar to virtual
public methods. They just don't
have a public function to call directly. The header
file contains only a declaration of the virtual function:
These virtual functions are often used to delegate part of the job
to child classes:
Again, it is possible to provide a default implementation for this
private virtual function:
Derived classes can then override the method with code such as: