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

Norman Vine nhv at cape.com
Tue Nov 12 15:00:48 EST 2002


David Blasby writes:
> 
> 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?

Nope !

In fact since this is what GEOS does internally I am rather surprised that it is
only twice as slow as the original JAVA implementation :-)

FWIW - I think this is due to the 'literal' translation of the code from Java where
'everything' is automatically passed by reference.

GEOS probably wants to change to where everything is passed by reference
like JTS does,  I am not really sure what is the best way to start dong this though.

It's tempting to just start with a new geom.h that defines an interface that is
tuned for FAST 'C' manipulation

Norman







More information about the geos-devel mailing list