several questions

Saaa empty at needmail.com
Sat Feb 16 15:15:49 PST 2008


Thanks again,

I don't think I ever override a function, should I make them final then, for 
optimizing purposes, or is final only for classes?



> Thanks!
> Still bit uncertain about the static array though :)
> But I think I understand static now, although I never use 'this' making 
> it a
> bit more difficult to understand :)
> If I say something like 'static int i=0;' within the main I can't use i 
> for
> anything else in the whole program afterwards.

A static variable inside a function (which I did not cover in my last
post), will keep its value after the function has exited, and even when
it's being called again.
This function:

void sillyFunc()
{
static timesRun;
timesRun++:
writefln(timesRun);
}

will print the numer of times it's been run, for instance.

> It makes variables into global variables :)
> What exactly is 'having a this reference' ?

When you instantiate a struct or class, some piece of memory is allocated
for that instantiation. When you run a non-static member function, the
'this' pointer points to this data, basically saying 'here I am, and my
(again, non-static) member variables are offset from here.'

Example:

class Foo
{
int bar;
void baz()
{
bar++; // equivalent of this.bar++
writefln(bar);
}
}

void main()
{
Foo f = new Foo(); // f now points to a memory location that will be used
f.baz(); // as 'this' pointer inside the function bar executed here.
}

> And what does final mean. (the attributes page doesn't say anything  about 
> it

Final means 'this function cannot be overrided.' Subclasses of whatever
class has a final function will not be able to implement their own
versions of the final function.

class Foo
{
final void doStuff()
{
writefln("Foo.doStuff();");
}
}

class Bar : Foo
{
void doStuff() // here the compiler will complain "cannot override final
function Foo.doStuff
{
writefln("Bar.doStuff();");
}
}

> :/ )
> Private is easy to understand, thanks.
> 




More information about the Digitalmars-d-learn mailing list