Returning a struct by reference
grauzone
none at example.net
Sat Mar 21 06:47:00 PDT 2009
Simon TRENY wrote:
> 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;
Then you write a getter that simply returns the field by value.
The D compiler will (hopefully) inline the getter function, so there
shouldn't be a disadvantage in performance.
Note: I think D2.0 wants to introduce ref-returns at some point in the
future.
>>> Regards,
>>> Simon
>>>
>
More information about the Digitalmars-d
mailing list