[geos-devel] Pass by reference in Java and C++

David Blasby dblasby at refractions.net
Tue Nov 12 13:25:53 EST 2002


Martin Davis wrote:

> You got it right, Norman - in Java *everything* is passed by reference, whereas in C++ you get a choice.    I think when Yury did the initial translation of the code he stuck very close to the Java way of doing things, partly because it's hard to know without understanding the code fairly well whether passing-by-reference is needed or not (i.e. sometimes you really do want an alias to a shared object).
>
> It would certainly help for anyone who has time to look at the code and see if there's obvious wins to be had by switching to pass-by-value in particular cases.

I'm just starting playing around with creating GEOS "Geometry" objects.  Here's a little snippet of code:

Geometry createGEOSPoint(POINT3D *pt)
{
 Coordinate *c = new Coordinate(pt->x, pt->y, pt->z);

 return geomFactory->createPoint(*c);
}


This causes the Coordinate to be copied (to the createPoint() constructor), and the resulting Geometry to be copied out of the function.  It would be much more efficient to:

Geometry *createGEOSPoint(POINT3D *pt)
{
 Coordinate *c = new Coordinate(pt->x, pt->y, pt->z);

 return geomFactory->createPoint(c);
}

NOTE:   Geometry *createPoint( *Coordinate)
       =>  geomFactory->createPoint() returning a Geometry* and taking a Coordinate*.

Or am I missing something?



Also, the CoordinateList should have a constructor like:

cl = new CoordinateList3D(POINT3D* points, int npoints);
cl = new CoordinateList2D(POINT2D* points, int npoints);

Where POINT3D is a simple {double x,y,z} struct, and POINT2D is a simple {double x, double y}.

I dont really want to take my list of 100,000 points, convert them all to Coordindates, then make a CoordinateList, then a LinearRing, copy the linearRing over to the Polygon creator, then make a copy of the polygon to actually return.  Seems like an lot of work.

Is this the way to actually construct Geometry objects?  Or am I missing something?


dave
ps. For the C/C++ non-programmers - Geometry* means Pointer-to-Geometry.





More information about the geos-devel mailing list