www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/17/08:19:41

From: <telford AT xenon DOT triode DOT net DOT au>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dynamic memory allocation
Date: 16 Apr 1998 13:23:06 GMT
Organization: Triode Internet
Lines: 77
Message-ID: <6h50nq$oae$4@hyperion.triode.net.au>
References: <352EDA1E DOT 7B0C55AF AT teccart DOT qc DOT ca> <352F708A DOT 352E AT plinet DOT com>
NNTP-Posting-Host: xenon.triode.net.au
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019