Returning a struct by reference

Simon TRENY simon.treny at free.fr
Sat Mar 21 06:41:54 PDT 2009


grauzone Wrote:

> Simon TRENY wrote:
> > Hi there!
> > 
> > I'm quite new at D and I'm still just playing with it, but there is a thing that I find currently missing. Sometimes, I'd like to be able to return a struct by reference and not by value. For example, in the following example:
> > 
> > struct Position {
> >    float x;
> >    float y;
> > }
> > 
> > class Object {
> >    private Position m_position;
> > 
> >    public Position position() {
> >       return m_position;
> >    }
> > }
> > 
> > I'd like to be able to write things like this: myObject.position.x = 43 to actually change the position of the object. But right now, since "position" is a struct, it is returned by value and not by reference, and then the previous instruction won't change the position of the object, but it will work on a copy of the position field.
> > 
> > 
> > Here is the solutions that I can see to this problem:
> > 
> > - Returning a pointer to the position: "public Position *position() { ... }", but I'd like to keep my code as free from pointers as possible.
> >  - Make "Position" a class and not a struct. That could be a solution, but then, when I'll do things like "Position pos = object.position; pos.x = 43;", it will effectively change the position of the object, which I wouldn't like with this syntax.
> > 
> > Actually, I'd like to be able to do a thing like this:
> >    public ref Position position() {
> >       return m_position;
> >    }
> > which would be the equivalent form to passing structs by reference in a parameter.
> > 
> > Is there a way to do this in D?
> 
> Yes. Make the variable public.
> 
> class Object {
> 	Position position;
> }
> 
> This code is even simpler than your's above. Incredible, isn't it?

Ok, but then, what if I'd like to make the variable "read-only"? i.e. preventing the user from writing things like this:
myObject.position = pos2;

> 
> > Regards,
> > Simon
> > 




More information about the Digitalmars-d mailing list