Pointers or copies?

Arlen Albert Keshabyan arlen.albert at gmail.com
Wed Dec 20 14:36:12 PST 2006

== Quote from Orgoton (orgoton at mindless.com)'s article
> 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?

RTM: use operator '&' to get its pointer.
e.g. framequeue.add(&this);

Anyway, I recommend you to use all objects by reference, no pointers at all.
object[] framequeue;
framequeue ~= this;
framequeue ~= another_object;

More information about the Digitalmars-d mailing list