Returning const ref (structs) in D

Stian stian.pedersen at gmail.com
Sat Nov 24 12:46:41 PST 2012


Okay, Im trying to wrap my head around how i can return a 
reference from a class.
Lets say I have some class Foo that stores a struct Bar that 
stores many bytes.

In C, i would have a function

const Bar & getBar(){return bar;}

In code, i could get a (const) reference to bar using
const Bar & v = foo.getBar();
or i may copy it, if i need to use it for something else
Bar v = foo.getBar();

How can i do the same in D? The manuals aren't crystal clear on 
this matter. Using in in parameter lists will give me a const 
reference, so that i avoid input copying, if I understand 
correctly.

But i want get const aliases to structs, and if i should pass 
that alias into a variable storing a struct, then i would want 
the copy semantics.


More information about the Digitalmars-d-learn mailing list