2.3 Methods


2.3 Methods

Although you haven't seen the class initializer for Media yet, you can now see how to define a simple method. Here are some important things to remember about methods:

  • Methods usually do not appear in class structure. Instead, method prototypes usually appear somewhere soon after the class structure.

  • A method's name should reflect the class name (for example, media_*() for Media ).

  • A method's first parameter is always an object (a structure of the instance class). Any remaining parameters are up to you.

  • In public methods, always check that the first parameter is actually a valid object of the method's class.

  • In addition, cast this object parameter after you do the check, because the object you get could be in a subclass.

  • Be careful about setting an object's attributes. Standard GTK+/GNOME practice dictates that all attributes are properties (see Section 2.4); use that system for setting attributes.

These considerations sound like a lot of fuss, but this example shows that it doesn't amount to much:

 void media_print_inv_nr(Media *object) {   Media *media;   g_return_if_fail(IS_MEDIA(object));   media = MEDIA(object);   g_print("Inventory number: %d\n", media->inv_nr); } 

Most public methods contain everything here but the last line ( g_print(...); ).




The Official GNOME 2 Developers Guide
The Official GNOME 2 Developers Guide
ISBN: 1593270305
EAN: 2147483647
Year: 2004
Pages: 108

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net