[Next] [Up] [Previous] [Contents] [Index]
Next: 8. Miscelleanous Features Up: NannyMUD LPC Previous: 6. Strings, Arrays and

Subsections


7. Inheritance and Overloading


7.1 Inheritance

LPC supports inheritance. Inheritance is a way of making the variables and functions of one object available to another without copying the actual code. The syntax for inheriting an object is inherit "filename"; where 'filename' is the filename of the object one wish to inherit. This statement must come before any variables and functions are defined. LPC allows the inheritance of multiple objects.

An object can, however, protect function and variables from access through inheritance by declaring them with the type modifier 'private'. The inheritance itself can be modified by the modifiers 'private' and 'static'. Only the 'static' modifier has any effects, and that is to mark the inherited variables as static.


7.2 Overloading

All inherited functions can be overloaded, or redefined, by the inheriting object. Redefining a function declared as having the type modifier 'nomask' triggers a load-time error.

The overloaded functions can still be accessed by prepending the function name by '::', ::function();. If you inherit several files that defines the same function, this will call 'function' in the last inherited object. By prepending the '::' by the filename, exactly which function is called can be controlled: filename::function.

As an example, consider three files, 'file1', 'file2' and 'file3', all of which defines the function 'foo()', which returns 1, 2 and 3 respectively:

  inherit "path1/file1";
  inherit "path2/file2";
  inherit "path3/file3";

  foo()
  {
    int i;

    i  = file1::foo(); // Call function foo() in first object.
    i += file2::foo(); // Call function foo() in second object.
    i += file3::foo(); // Call function foo() in third object.
                       // This could as well have been written as
                       // i += ::foo();
                       // since 'file3' is the last inherited.
    return i;
  }
would then return 6, the sum.


[Next] [Up] [Previous] [Contents] [Index]
Next: 8. Miscelleanous Features Up: NannyMUD LPC Previous: 6. Strings, Arrays and
Mats Henrik Carlberg
1998-03-25