several questions

Derek Parnell derek at psych.ward
Sat Feb 16 15:38:31 PST 2008


On Fri, 15 Feb 2008 20:53:21 +0100, 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?


When a function's parameter is either a dynamic (variable-length) array or
an object, the function does not get a copy of the parameter's data given
to it. Instead it gets the address of the array/object instance. Actually,
for arrays the function gets both the address and the length. 

This is in contrast with structs, static (fixed-length) arrays, and basic
data types. In these cases, the function does get a copy of the parameter's
data.

> 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:)

Exactly (almost). Use this instead ...


   void func(ref X[n] array)

Where 'X' is the array's datatype and 'n' is its fixed length. There is no
need for the '.ptr' property when also using 'ref'. An alternative is to
use pointers directly, but then you loose array-bounds checking.

   void func( X* array) 


> Another question :)
> 
> What is the advantage of making a function/variable static?
> It makes it nonvirtual, right. Why is this good?
> When should I make something static or final?

I assume by "function/variable" here, you are referring to a class member. 

You would make a function member 'static' if you wanted to call it without
using a specific object. It belongs to the class and not a class instance
(object). It is kind of like a free function that is scoped to the class's
namespace.

    class Foo 
    {
        static void Bar() { . . . }
    }
    . . .
    Foo.Bar();  // Call the Foo class's Bar function.

You would make a variable member 'static' if you wanted to use it without
requiring a specific object. It belongs to the class and not a class
instance (object). It is kind of like a global variable that is scoped to
the class's namespace.

    class Foo 
    {
        static int nextid = 1;
        int myID;
        this() { synchronized {myId = nextid; nextid++;} }
    }
    . . .
    Foo A = new Foo; 
    Foo B = new Foo;
    writefln("A=%s, B=%s, next=%s", A.myID, B.myID, Foo.nextid);

    // Displays "A=1, B=2, next=3"

 
> A private function/var can't used by anything in a 'lower' scope, right?

I'm not sure what you mean by 'lower' scope. 

A private function/variable can only be accessed from code within the same
module as its declaration. 

However there is a bug, which has been around for years but Walter doesn't
regard as urgent, where 'private' can be overturned if the reference to the
private function/variable is fully qualifed. In that case, code from any
module can access the "so called private" entity.

   // -- file AA.d ---
   private int c;

   // -- file BB.d ---
     c = 1; // correctly fails.
     AA.c = 2; // Incorrectly works.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list