Smarter overloading method? - Printable Version +- Colobot Forum - International Colobot Community (https://colobot.info/forum) +-- Forum: [Archive] New forum (2015-2019) (https://colobot.info/forum/forumdisplay.php?fid=76) +--- Forum: Colobot: Gold Edition Basics (https://colobot.info/forum/forumdisplay.php?fid=58) +---- Forum: Help (https://colobot.info/forum/forumdisplay.php?fid=61) +---- Thread: Smarter overloading method? (/showthread.php?tid=965) |
Smarter overloading method? - Smok - 02-02-2018 I made this. 3 simple functions with overloads to cover all possible parameter combinations. The problem is that code lenght scales expotentially with number of parameters. Is there smarter/shorter way to make function with parameters that can be different types? Code: extern void object::Libr(){} RE: Smarter overloading method? - DomesticAnt - 03-24-2018 You can encapsulate your arguments into classes using multiple constructors. It's still fairly ugly, but rather than scaling exponentially on the number of arguments the complexity scales linearly on the number of types of argument. Here's a trivial example using two arguments of the same type (a location specified as an object, a point or a category): Code: public class Thing { You can see that the overloaded Thing() constructors effectively convert each argument to a Point as the time that the function is called, so I don't have to overload the function at all. If I wanted to add a fourth acceptable argument type, I'd just add another Thing constructor. |