March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Could anybody explain what exactly the difference between a class allocator and constructor is? |
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | erm.. Class[] className; className.length=10; className.function; results in runtime access violation. Why? |
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | >
> Class[] className;
> className.length=10;
> className.function;
may be this?
Class[] className;
className.length=10;
className[0]=new Class();
className[0].function;
|
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | > may be this?
>
> Class[] className;
> className.length=10;
> className[0]=new Class();
> className[0].function;
>
Yes it is :)
What then does Class[] className do exactly? Or, why do I need to use new?
|
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | "Saaa" <empty@needmail.com> wrote in message news:fqjjn5$2jb6$1@digitalmars.com... >> may be this? >> >> Class[] className; >> className.length=10; >> className[0]=new Class(); >> className[0].function; >> > > Yes it is :) > > What then does Class[] className do exactly? Or, why do I need to use new? > The same reason: Class class; class.function; doesn't work. Because all class variables are references which point to nothing until you use 'new'. A Class[] is an array of references to class instances which is filled with nulls by default. You have to actually initialize the array by creating instances of the class. Class[] arr; foreach(ref c; arr) c = new C; |
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Thanks,
Why isn't it initialized?
I mean, when are these null referenced class pointers useful?
>>> may be this?
>>>
>>> Class[] className;
>>> className.length=10;
>>> className[0]=new Class();
>>> className[0].function;
>>>
>>
>> Yes it is :)
>>
>> What then does Class[] className do exactly? Or, why do I need to use new?
>>
>
> The same reason:
>
> Class class;
> class.function;
>
> doesn't work. Because all class variables are references which point to nothing until you use 'new'.
>
> A Class[] is an array of references to class instances which is filled with nulls by default. You have to actually initialize the array by creating instances of the class.
>
> Class[] arr;
>
> foreach(ref c; arr)
> c = new C;
>
|
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | "Saaa" <empty@needmail.com> wrote in message news:fqjknh$2lrm$1@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". |
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
> Thanks,
> Why isn't it initialized?
> I mean, when are these null referenced class pointers useful?
>
Two things.
First, a null reference is free to set up, whereas instantiating a class is not free.
Second:
---
class Bicycle {
Human owner;
}
auto bike = new Bicycle();
---
The bike doesn't have an owner. Do you want to create a Human object that refers to nobody? Well, maybe, for some purposes, but not necessarily. That would probably alter your definition of Human.
|
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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".
>
|
March 04, 2008 Re: oop tutorials | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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".
>>
>
>
|
Copyright © 1999-2021 by the D Language Foundation