oop tutorials
Jarrett Billingsley
kb3ctd2 at yahoo.com
Tue Mar 4 06:55:28 PST 2008
"Saaa" <empty at needmail.com> wrote in message
news:fqjknh$2lrm$1 at digitalmars.com...
> Thanks,
> Why isn't it initialized?
> I mean, when are these null referenced class pointers useful?
The same time any null pointer is useful - when you want to use it as a
sentinel for some reason.
D is certainly not unique in this regard. In all the languages I've ever
used or seen, not one will automatically allocate an object when you declare
a reference or pointer to one.
Maybe you're getting confused by C++ (I don't know your background) where:
Class c;
c.foo();
is legal, but something entirely different is happening here. This is more
like a D struct, where the class is allocated on the stack, not the heap.
In D:
struct Struct
{
void foo() {}
}
...
Struct s;
s.foo();
The D code:
Class c = new Class;
c.foo();
Translates to:
Class* c = new Class();
c->foo();
in C++. Again, C++ will not automatically allocate a new class if you just
write "Class* c".
More information about the Digitalmars-d-learn
mailing list