What on earth is a ref function?

Mike Parker aldacron at gmail.com
Sun Aug 8 22:01:16 PDT 2010


simendsjo wrote:
> The spec is very short here, and the example doesn't give me much..
> 
> // I thought "allows functinos to return by reference" meant it could 
> return local variables..
> ref int* ptr() {
>     auto p = new int;
>     *p = 12;
>     return p; // Error: escaping local variable
> }
> 

For free functions, it's useful for returning references to module or 
global scope variables. Though what the use-case for that would be, I 
don't know off the top of my head. In C++, it's a common idiom for class 
methods to return references, rather than pointers, to class members in 
certain situations.


More information about the Digitalmars-d-learn mailing list