Problem with Point property

Henning Hasemann hhasemann at web.de
Tue Mar 6 13:29:14 PST 2007


Hi all, say I have a structure Point and an attribute position like this:

struct Point {
  int x, y;
}

class Foo {
  Point position;
}

So far, so good. Now I want to substitude the attribute with a property
(because I want to react on changes of the position immediately)
The setter is no Problem:

  void position(Point p) { mPosition = p; react(); }

The getter indeed is. If I wrote:

  Point position() { return mPosition; }

this would break expressions like

Foo().position.x = 5;

which I happen to use often.
The solutions I see are

* Return a proxy object instead of a real Point.
  Bad because the return type is not Point anymore.

* Return a Point*, would break types too I dont
  know if these two even work.

* Turn Point into a class so I return a reference.
  Possible, but Point really just carries 2 integers
  and I either had to change all "Point(...)"'s into "new Point(...)"'s,
  or implement opCall for a class which I'm not sure if it is the best
  option, since it may be misleading.

Are there other ways? Can I somehow return a reference to a struct
without "leaving" the type?

What would you do?

TIA,
Henning

-- 
v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR hackerkey.com



More information about the Digitalmars-d mailing list