Why can't we make reference variables?
Tommi
tommitissari at hotmail.com
Wed Aug 29 05:41:25 PDT 2012
On Wednesday, 29 August 2012 at 02:57:27 UTC, Era Scarecrow wrote:
> Assuming 'ref' works:
>
> struct S {
> ref int r;
> }
>
> //ref local variable/stack, Ticking timebomb
> //compiler may refuse
> void useRef(ref S input, int r) {
> input.r = r;
> }
I think we might be talking about somewhat different things. What
I mean by reference variable is what the term means in C++. From
wikipedia: http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
C++ references differ from pointers in several essential ways:
* It is not possible to refer directly to a reference object
after it is defined; any occurrence of its name refers directly
to the object it references.
* Once a reference is created, it cannot be later made to
reference another object; it cannot be reseated. This is often
done with pointers.
*References cannot be null, whereas pointers can; every reference
refers to some object, although it may or may not be valid. Note
that for this reason, containers of references are not allowed.
* References cannot be uninitialized. Because it is impossible to
reinitialize a reference, they must be initialized as soon as
they are created. In particular, local and global variables must
be initialized where they are defined, and references which are
data members of class instances must be initialized in the
initializer list of the class's constructor. For example:
int& k; // compiler will complain: error: `k' declared as
reference but not initialized
More information about the Digitalmars-d
mailing list