Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Smarter overloading method?
#2
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 {
    void Thing( point np ) {
        p = np;
    }
    void Thing( object thething ) {
        p = thething.position;
    }
    void Thing( int category ) {
        object thething = radar(category);
        p = thething.position;
    }
    
    point p;
}

float distanceofthings( Thing a, Thing b ) {
    return distance( a.p, b.p );
}

extern void object::Main()
{
    object item = radar(Me);
    point p = item.position;
    
    float d = distanceofthings( new Thing(this), new Thing(p) );
    message( "Distance to player avatar " + d );
    
    d = distanceofthings( new Thing(this.position), new Thing( SpaceShip ));
    message( "Distance to spaceship " + d );
}

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.


Messages In This Thread
Smarter overloading method? - by Smok - 02-02-2018, 01:29 PM
RE: Smarter overloading method? - by DomesticAnt - 03-24-2018, 01:57 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)