Jump to content

[SOLVED] Constructor Overloading


photo

Recommended Posts

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

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

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

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

 

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
×
×
  • Create New...