LDMud logo: an emblem with three snakes, on the left behind that emblem a cogwheel. In front of both is a sword.

LDMud

Home of LDMud

View the Project on GitHub ldmud/ldmud

Inheritance

Objects consist of variables and functions which are written in one file per object blueprint. But it would be wearisome if we had to write every function anew just because we'd like to have a torch beside a lamp. That's where inheritance comes into play. The inherit statement allows to include all the variable declarations and functions of a blueprint into another object. After that you can modify the behaviour by overloading functions.

    inherit "/i/item";

int take_it(string arg)
{
write("You can't take this!\n");
return 1;
}

The inherit statement must come before any variable or function declaration. If there is no blueprint for the given file name then this file will be compiled first. Also there may be more than one inherit statement to combine functionality from several blueprints

Access to inherited variables and functions

When inheriting the variables and functions of a blueprint their modifiers are applied. That means as private declared variables or functions are not accessible for the inheriting object. protected functions however are. It's also possible to add modifiers to all inherited variables and functions:

    private functions nosave variables inherit "/i/item";

But it's not possible to make private variables or functions public or vice-versa.

Overloading

After having inherited a lot of functions it's possible to overwrite some of them simply by defining functions with them same name in the object's file. Whenever the function with this name is called (even by other inherited functions) then your new function will be called instead. This is called overloading.

Nevertheless you can explicitly call the original inherited function by prepending the function name with ::.

    int take_it(string arg)
{
write("You slowly approach this item.\n");
return ::take_it(arg);
}

This calls take_it in the first inherited blueprint that defines this function. You can specify the inherited blueprints name to avoid ambiguitiy:

    int take_it(string arg)
{
write("You slowly approach this item.\n");
return item::take_it(arg);
}

To call a function in all inherited programs use "*"::fun.