Ref parameter: intended behavior or bug?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Wed Aug 22 03:26:03 PDT 2007


Mike Parker wrote:
> Bill Baxter wrote:
>> 'ref' is not a type constructor in D, it is a storage class.
>> .sizeof gives the size of the type.  The storage class shouldn't 
>> affect that.
> 
> But it's a bit inconsistent, is it not? Consider this function:
> 
> void structPtr(MyStruct* ms)
> {
>     writefln("Struct ptr: %d", ms.sizeof);
> }
> 
> This will print 4. Why should the behavior of 'ref' be any different 
> than that of '*'? If I want the size of the type, I would use 
> MyStruct.sizeof.

Because 'ref' also adds extra '*'s in front of every place the variable 
occurs, so your code with 'ref' is more like
-----
void structPtr(ref MyStruct ms)
{
     writefln("Struct ptr: %d", (*ms).sizeof);
}
-----
so you print the size of the struct referred to, not the size of the 
reference itself.


More information about the Digitalmars-d-learn mailing list