MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | CONCEPT |
| 2 | overloading |
| 3 | |
| 4 | DESCRIPTION |
| 5 | This concept is strongly connected with the concept of inheritance. |
| 6 | A function is called 'overloaded' if it is defined more than once |
| 7 | in an object. This can happen if the object inherits other objects |
| 8 | which have defined a function with the same name. |
Zesstra | 7ea4a03 | 2019-11-26 20:11:40 +0100 | [diff] [blame] | 9 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 10 | Usually the overloading is wanted and intended by the inheriting |
| 11 | object to change the behaviour of the function it overloads. |
| 12 | To call the overloaded functions from the overloading object the |
| 13 | ::-operator is used. |
Zesstra | 7ea4a03 | 2019-11-26 20:11:40 +0100 | [diff] [blame] | 14 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 15 | From outside the object only one of the functions can be called |
| 16 | via call_other() or the like; this will be the topmost of all |
| 17 | overloaded functions. |
| 18 | |
| 19 | Normally an overloading function is declared the same way as the |
| 20 | overloaded function, this means it has the same number and types |
| 21 | of arguments. If an object wants to change the behaviour of the |
| 22 | function in a way that it can get more arguments than the original |
| 23 | function, it has to use the modifier 'varargs' or a compiler error |
| 24 | will be raised. |
| 25 | |
| 26 | EXAMPLE |
| 27 | File /players/alfe/a.c: |
| 28 | |
| 29 | foo() { write("A"); } |
| 30 | |
| 31 | File /players/alfe/b.c: |
| 32 | |
| 33 | foo() { write("B"); } |
| 34 | |
| 35 | File /players/alfe/c.c: |
| 36 | |
| 37 | inherit "players/alfe/a"; |
| 38 | inherit "players/alfe/b"; |
| 39 | |
| 40 | foo() { |
| 41 | a::foo(); |
Zesstra | 7ea4a03 | 2019-11-26 20:11:40 +0100 | [diff] [blame] | 42 | b::foo(); |
| 43 | write("C"); |
| 44 | } |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 45 | |
Zesstra | 7ea4a03 | 2019-11-26 20:11:40 +0100 | [diff] [blame] | 46 | To call "players/alfe/c"->foo() will now result in the output of |
| 47 | ABC. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 48 | |
| 49 | SEE ALSO |
| 50 | modifiers(LPC), inheritance(C), functions(LPC) |