Mail Archives: djgpp/1998/04/17/08:19:41
In comp.os.msdos.djgpp Charles Terry <cterry AT plinet DOT com> wrote:
> J-Réginald Louis wrote:
> >
> > My question is not about DJGPP itself, but a general C++ question.
> > I'm writting a 3D engine. I have a class called 'Scene' holding a list
> > of object to render.
> > The list is consisting of an array of pointers to type 'Object'. Like
> > this:
> >
> > So, what I'm asking is how can I append and remove objects in the list
> > dynamicaly?
> Have you been to comp.lang.c++ ?
Oh yes, one other point. If you have allocated memory
inside an object then you must declare a destructor for that
object and a copy constructor and an operator =() function.
The copy constructor is required when constructing one
object out of another like:
MyObj x;
MyObj y = x;
The operator=() function is used for any other assignment
from one object to another and has to clean up the old
allocation before copying the new allocation. These things
are usually mentioned in C++ text books but you have to know
to look for them, otherwise you won't realise their importance.
- Tel
> // #define MAX_LIST 10 // example
> struct List{
> Object* curobject ;
> list* nextlist ;
> };
> stuct Scene
> {
> list *ObjList;
> char empty=TRUE;
> void AddObject(Object *obj); // Add an object to the
> > int RemoveObject(Object *Obj); // Remove the object from the
> > // Other data definition and method definition
> > };
> void Scene::AddObject(Object*obj)
> {
> list * temp=ObjList;
> list * newobject=new list;
> newlist->curobject=obj;
> newlist->nextlist=NULL;
> if (empty){ObjList=newlist; empty=FALSE;}
> else{
> while (temp->nextlist|=NULL){
> temp=nextlist;}
> temp->nextlist=newlist;
> }}}
> The remove function is a little tougher so you get
> that one.
> Psuedocode:
> step thru the list
> compare the pointer in the list with the pointer you want to
> remove
> when you find it make the prev nextlist pointer point
> to the current nextlist pointer.
> delete the current list
> check for empty.
> Charles
- Raw text -