How do you take the address of a struct in D?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 5 16:02:33 PST 2016


On 02/05/2016 03:53 PM, Enjoys Math wrote:
> SDL_RenderCopy(...) takes two pointers to SDL_Rect's,
>
> I have a property method in another class returning the SDL_Rect
> equivalent of a Box (my structure).

Returning by reference or by value?

 > Taking ampersand on the left of a
> call to the property does not give the address (&).

Does it compile and produce an incorrect result? For it to compile, the 
returned object must be an lvalue. The following works (not the 
commented-out part):

import std.stdio;

struct S {
}

S s;

S returnsCopy() {
     return s;
}

ref S returnsRef() {
     return s;
}

void main() {
     // auto addr = &returnsCopy();
     auto addr = &returnsRef();
     writeln(addr);
}

Ali




More information about the Digitalmars-d-learn mailing list