cem.orsel Posted November 13, 2014 Share Posted November 13, 2014 Hi, is constructor overloading possible in UnigineScript? I can create a default constructor with no arguments passed as an overload, but when I define 2 constructors (one having a Node argument; the other, string), I get the error "different argument prototype" (even though I don't use a prototype, but declaration directly). Link to comment
angus.wood Posted November 13, 2014 Share Posted November 13, 2014 You can overload but only with different argument counts from what I can see. class Foo { Foo(string test) { log.message("constructor 1\n"); } Foo(string one, int test) { log.message("constructor 2\n"); } }; void main() { Foo p = new Foo("xx"); Foo r = new Foo("yy",1); } Link to comment
cem.orsel Posted November 13, 2014 Author Share Posted November 13, 2014 The methods are parsed according to their argument counts then... Weird. Thanks, I'll try that. Link to comment
cem.orsel Posted November 13, 2014 Author Share Posted November 13, 2014 This is basically what I'm trying to do. I have increased the argument count and given an extra dummy int argument to the second overload. It could not recognize the token "argFileName" then. I don't get how it does not recognize the passed argument. class Firefighter { private: Node parentNode; public: Firefighter(Node argNode) { parentNode = argNode; } Firefighter(string argFileName) { parentNode = getNodeByReference(argFileName); } }; Link to comment
frustum Posted November 13, 2014 Share Posted November 13, 2014 Just select the argument type manually: Firefighter(int arg) { if(arg is string) { parentNode = getNodeByReference(arg); } else if(arg is Node) { parentNode = arg; } else { throw(__FUNC__ + ": unknown argument %s\n",typeinfo(arg)); } } Link to comment
ulf.schroeter Posted November 14, 2014 Share Posted November 14, 2014 I don't get how it does not recognize the passed argument. UNIGINE script uses dynamic typing. Each function argument variable in fact is a container variable (think of it as a variant). All argument type indications (e.g. int/string/Node etc) are just textual HINTS, but actually not different types. Therefore your "overloaded" constructor definitions appear to the UNIGINE script compiler like redundant definitions class Firefighter { private: VARIANT parentNode; public: Firefighter(VARIANT argNode) { ....; } Firefighter(VARIANT argFileName) { ....; } Link to comment
cem.orsel Posted November 18, 2014 Author Share Posted November 18, 2014 Oh I get it now, it also explains how we can pass an integer and treat it as string or node. Thanks. Link to comment
Recommended Posts