several questions

Simen Kjaeraas simen.kjaras at gmail.com
Fri Feb 15 12:46:29 PST 2008


On Fri, 15 Feb 2008 20:53:21 +0100, Saaa <empty at needmail.com> 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?

It means that when used as a parameter to a function, a reference type  
will just send (copy, if you will) a pointer to itself to the function,  
while a value type will copy all its data. 'Least I think that should be  
fairly correct.

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

I seriously doubt ut does. Not having checked, I won't say that with 100%  
certainty, though.

> 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?

A static function or variable becomes a member of the class/struct  
definition, instead of of the instance. Say you have

class Foo
{
	static int bar_static;	// Shared by all instances of Foo, so if one of  
them changes the value, it's changed for all.
	int bar;		// New int specific to each instance. Change this value, and no  
other instance will notice.
	static void doStuff(){}	// Function with no 'this' pointer. Can be called  
using Foo.doStuff(), with no instance of Foo anywhere in your program. Can  
change only static members of Foo.
	void doStuff(){}	// Function with 'this' pointer. Needs an instance on  
which to work. Can change static members of Foo, as well as normal members.
}

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

Private in D means 'not accessible outside this module.' So this is  
perfectly legal:

class Foo
{
	private int foo;
}

class Bar : Foo
{
	void bar()
	{
		foo = 3;
	}
}

void doStuff()
{
	Foo f = new Foo();
	f.foo = 4;
}

You can however not change foo from inside a different source file  
(module).

> Well thats it for now =D


More information about the Digitalmars-d-learn mailing list