by Kzer-Za » Mon May 25, 2020 12:11 am
It would be useful if user variables could be accessed and changed from a class that is not inherited from the same class. (If the variable really needs to be inaccessible, it can always be declared protected or private).
I have tried creating a "library" class with static functions, which would be used by any class (viewtopic.php?f=122&t=68675), but I can't access user variables in an unrelated class from such a function.
Why this is a problem? For example, I may need to call one and the same function from a player and from a monster, and obviously I can't inherit a monster and a player from one class. There are other cases when inheriting two classes from the common ancestor is more trouble than it's worth, but they need to call the same function.
Why don't use separate functions with the same code for them? Because if/when I decide to expand/change that code I may change it in one place and forget about the other(s). It seems like a bad design to keep one code in more than one place.
BTW, I need it for more than the simple function in the topic the link to which I has given, it's just a start. Besides, others may need it too. And since the security concerns are covered by the possibility to declare variables as protected or private, it seems reasonable that normal variables should be accessible from anywhere.
It would be useful if user variables could be accessed and changed from a class that is not inherited from the same class. (If the variable really needs to be inaccessible, it can always be declared protected or private).
I have tried creating a "library" class with static functions, which would be used by any class (viewtopic.php?f=122&t=68675), but I can't access user variables in an unrelated class from such a function.
Why this is a problem? For example, I may need to call one and the same function from a player and from a monster, and obviously I can't inherit a monster and a player from one class. There are other cases when inheriting two classes from the common ancestor is more trouble than it's worth, but they need to call the same function.
Why don't use separate functions with the same code for them? Because if/when I decide to expand/change that code I may change it in one place and forget about the other(s). It seems like a bad design to keep one code in more than one place.
BTW, I need it for more than the simple function in the topic the link to which I has given, it's just a start. Besides, others may need it too. And since the security concerns are covered by the possibility to declare variables as protected or private, it seems reasonable that normal variables should be accessible from anywhere.