www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Keeping list of Objects

reply Jeremy <Jeremy_member pathlink.com> writes:
I'm writing a game in D, and in a loop, I'm creating new objects and keeping
track of them by storing their pointers:

e.g. I have a linked list of pointers. In the loop, I do a "OBJECT newThing =
new OBJECT()", and then I add the pointer &newThing to the linked list. After
that function exits (and the scope exits), I try to access these OBJECTs by
referencing the pointers... but I get an error. I assume the garbage collector
is freeing that space?

Anyway, I want to do this because I want a linked list of different objects
(e.g. all the objects in my game, which might be different classes -- I can cast
these pointers later).

I'm realizing that this won't work (unless I make the newThing global, then the
data I allocated still exists later). Anyone have a better way of accomplishing
the same thing?
Apr 27 2006
next sibling parent reply Jeremy <Jeremy_member pathlink.com> writes:
I fixed my own problem -- I just need not use * and & symbols :)

Sometimes I get confused on to what is actually already made a pointer, and what
is not.

In article <e2ruoi$nv8$1 digitaldaemon.com>, Jeremy says...
I'm writing a game in D, and in a loop, I'm creating new objects and keeping
track of them by storing their pointers:

e.g. I have a linked list of pointers. In the loop, I do a "OBJECT newThing =
new OBJECT()", and then I add the pointer &newThing to the linked list. After
that function exits (and the scope exits), I try to access these OBJECTs by
referencing the pointers... but I get an error. I assume the garbage collector
is freeing that space?

Anyway, I want to do this because I want a linked list of different objects
(e.g. all the objects in my game, which might be different classes -- I can cast
these pointers later).

I'm realizing that this won't work (unless I make the newThing global, then the
data I allocated still exists later). Anyone have a better way of accomplishing
the same thing?

Apr 27 2006
parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
Jeremy wrote:
 I fixed my own problem -- I just need not use * and & symbols :)
 
 Sometimes I get confused on to what is actually already made a pointer, and
what
 is not.
 

That's probably because you come from a C++ background. I think that if you're used to Java, then you won't make this mistake. Anyway, in D, objects (of classes) are always by reference, never by value!! never! structs default to by-value, but if they are an inout parameter in a function, I believe they will be references.
 In article <e2ruoi$nv8$1 digitaldaemon.com>, Jeremy says...
 
I'm writing a game in D, and in a loop, I'm creating new objects and keeping
track of them by storing their pointers:

e.g. I have a linked list of pointers. In the loop, I do a "OBJECT newThing =
new OBJECT()", and then I add the pointer &newThing to the linked list. After
that function exits (and the scope exits), I try to access these OBJECTs by
referencing the pointers... but I get an error. I assume the garbage collector
is freeing that space?

Anyway, I want to do this because I want a linked list of different objects
(e.g. all the objects in my game, which might be different classes -- I can cast
these pointers later).

I'm realizing that this won't work (unless I make the newThing global, then the
data I allocated still exists later). Anyone have a better way of accomplishing
the same thing?


Apr 28 2006
prev sibling parent tom <tom_member pathlink.com> writes:
In article <e2ruoi$nv8$1 digitaldaemon.com>, Jeremy says...
I'm writing a game in D, and in a loop, I'm creating new objects and keeping
track of them by storing their pointers:

e.g. I have a linked list of pointers. In the loop, I do a "OBJECT newThing =
new OBJECT()", and then I add the pointer &newThing to the linked list. After
that function exits (and the scope exits), I try to access these OBJECTs by
referencing the pointers... but I get an error. I assume the garbage collector
is freeing that space?

Anyway, I want to do this because I want a linked list of different objects
(e.g. all the objects in my game, which might be different classes -- I can cast
these pointers later).

I'm realizing that this won't work (unless I make the newThing global, then the
data I allocated still exists later). Anyone have a better way of accomplishing
the same thing?

why are you using a pointer to an object? i am almost certian that is your problem. you are probably storing the address of your object reference. just use a list of references, or I think you have to cast to a pointer.
Apr 27 2006