Pointers or copies?
Hasan Aljudy
hasan.aljudy at gmail.com
Thu Dec 21 05:40:50 PST 2006
In addition to what the others have said, here are some more tips:
* All D class inherit from Object by default. So, if your "object" class
doesn't do anything special, you can ditch it, and use the standard
Object class.
* You can iterate over an array using foreach:
Object[] objs ..
foreach( Object object; objs )
{
//do something
}
even better, foreach has magical type deduction, so you don't have to
say "foreach Object object", just say:
foreach( object; object_list ) { .. }
The compiler will automatically deduce the type of "object" based on
"object_list".
* You can remove an element from the array using a slicing trick:
void remove( Object[] array, int i )
{
array = array[0..i] ~ array[i+1..$];
}
I haven't tested this but it should work, although I suspect it maybe
suboptimal.
The $ inside the slice refers to array.length.
Orgoton wrote:
> I'm making a game and I have to keep track of the objects in the game, which
> inherit from a master object class called "objct". So I would make a class for
> "baddie", "player" and such. Now, to draw them I have to keep track of their
> existance, so, when one is created, it adds itself to the "frame queue" and
> removes itself when it is deleted. To keep track of them I created a "queue"
> class, which has a protected member called "objct *pters[]". It is an array of
> pointers so when I recieve a new actor for the queue, I increase the array
> size accordingly and on the next slot, just add it there. Simple. Now, the
> queue class has a method called "add" and "remove" both of them take as
> parameter "objct *target".
>
> To sum it up and finally tell you my question the objct has in it's
> constructor "framequeue.add(this);" and a correspondind "remove(this)".
>
> Apparently, "this" is not a pointer... as the compiler claims: "function
> queue.remoce (objct *target) does not match parameter types (objct)" and that
> "cannot implicitly convert expression (this) of type objct to objct*". So,
> does passing "this" create a copy of the object? How do I pass the pointer to
> the object to the queue? Any other solution?
More information about the Digitalmars-d
mailing list