Note that there are some explanatory texts on larger screens.

plurals
  1. POConstructor that can either make a copy or reference
    primarykey
    data
    text
    <p>The code below shows a rectangle class using <code>double</code> points, which are also stored in an object. The rectangle and the points in the rectangle are immutable because they do not need to change. I would like to provide the ability to copy (make new point objects) or make a reference to the points provided in the constructor, but the only way I could think of doing this is to add a boolean parameter specifying if the caller wants to make a copy or reference. </p> <p>This is for extensibility, although it may not be of prime importance, I would like this option. However, I don't like the way it's implemented with the boolean parameter. Is there a way I can make two constructors taking the same parameters, one to make a reference and one to make copies? or is there an equivalent to C++ parameter auto definition in the prototype so it doesn't need to be specified by the caller? I have thought about using varargs, but then the caller could send unlimited parameters as garbage, possibly causing a stack overflow, I think...</p> <pre><code>/** * An immutable, double-precision floating-point (64-bit doubles x4) rectangle. * The rectangle can be made to reference existing points, or to create new points. Since the points * are also immutable, this is acceptable, as it is guaranteed they cannot change. * @author Bill */ public class DoubleRect { public final DoublePoint topLeft; public final DoublePoint bottomRight; public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight, boolean makeCopies) { if(makeCopies == true) { topLeft = new DoublePoint(setTopLeft); bottomRight = new DoublePoint(setBottomRight); } else { topLeft = setTopLeft; bottomRight = setBottomRight; } } } </code></pre> <p><strong>UPDATE: Thanks to all that helped me figure out what to do instead. This is how I recoded it.</strong></p> <pre><code>/** * An immutable, double-precision floating-point (64-bit) rectangle. * The rectangle can be made to reference existing points, or to create new points. Since the points * are also immutable, referencing the points is acceptable, as it is guaranteed they cannot change. * @author Bill */ public class DoubleRect { public final DoublePoint topLeft; public final DoublePoint bottomRight; /** * This constructor will reference the passed objects rather than duplicating them. * See the static factory method createWithClonedPoints() for making internal copies of the point objects * @param setTopLeft Double point designating the top left coordinate * @param setBottomRight Double point designating the bottom right coordinate */ public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight) { topLeft = setTopLeft; bottomRight = setBottomRight; } /** * This constructor will create new immutable points within this object using the coordinates specified */ public DoubleRect(double top, double left, double right, double bottom) { topLeft = new DoublePoint(left, top); bottomRight = new DoublePoint(right, bottom); } public static DoubleRect createWithClonedPoints(DoublePoint topLeft, DoublePoint bottomRight) { return new DoubleRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload