oop tutorials
Ary Borenszweig
ary at esperanto.org.ar
Tue Mar 4 07:10:27 PST 2008
What is Class doesn't have a default constructor, or has many?
Saaa wrote:
> I'm sorry, I've never used a null pointer (as far as I know) nor programmed
> in any oo style before.
> I just thought that it would work like structures and if I wanted to have a
> nullpointer you could do something like:
>
> Class[] className=void;
>
> As I thought that most of the time you just want an instance and not just a
> reference to nothing.
> Thats why I asked why other people wanted this null pointer.
>
>
>
>> 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