several questions

downs default_357-line at yahoo.de
Fri Feb 15 14:19:23 PST 2008


Saaa wrote:
> From the D1 documentation:
> For dynamic array and object parameters, which are passed by reference, 
> in/out/ref apply only to the reference and not the contents.
> 
> What exactly (internally memory wise) does 'passing' mean. Is it like 
> copying?


int x = 4;
// the value 4, assigned to x, is now outside of test.

test(x); // here I pass the value 4, in the form of x, into test.

int test ( int a )
{
 // 4, in the form of a, has been passed into test.

> And does this mean that static arrays are not passed by reference and should 
> I use:
> void func(ref array.ptr)
> ,because otherwise the whole array is passed (copied) ? (which sound slow:)
> 

Complete misunderstanding.
An array is basically this with syntax candy:

struct array (T)
{
	size_t length;
	T* ptr;
}

Now, that paragraph from the documentation basically means, that if you pass an array into a function, you really just pass its length and ptr.
So all these keywords, ref, out, in .. apply to the ptr and length, not the array's contents.

Basically, an array is a reference to the data, with additional length information.
That's what's meant by "dynamic array [...] parameters [...] are passed by reference".

> Another question :)
> 
> What is the advantage of making a function/variable static?
> It makes it nonvirtual, right. Why is this good?
Because it can be inlined.

> When should I make something static or final?
> 
When you don't want it to be virtual :)

 --downs


More information about the Digitalmars-d-learn mailing list