Mail Archives: djgpp/1999/10/01/15:39:15
My question is:How do I integrate a dynamical linked list system into
another class?
let me show you some code to illustrate my problem:
class COORDINATE;
COORDINATE *coordinate; //GLOBAL variable
//this is the actual
problem, if I dont want this to be a global variable.
//Here comes the definition:
class COORDINATE
{
public:
//object data
int x,y;
//linked list stuff:
COORDINATE *prev, *next;
//constructor
COORDINATE(int a, int b)
{
x=a;
y=b;
prev=NULL;
next=coordinate;
next->prev=this;
coordinate=this;
}
//destructor
~COORDINATE()
{
x=0;
y=0;
if (prev!=NULL) prev->next=next;
else
coordinate=next; //if this line is executed it means that the
global "coordinate" was deleted
}
}
int main()
{
//Now, the only thing i have todo to create a new COORDINATE object is
for example:
new COORDINATE(5,5);
new COORDINATE(6,7);
new COORDINATE(3,5);
//for example now mabey I want to delete all objects that has y set to
5
COORDINATE *save_next,*p=coordinate;
while(p!=NULL)
{
save_next=p->next;
if (p->y==5) delete p;
p=save_next;
}
}
----------------------------------------------------------------------------
--------------------------------------------------------------------------
All of this code should work, but what if would want to have the
"coordinate" inside another class eg.:
class STARFIELD
{
COORDINATE *star_coordinates
// followed by some other stuff
.
.
.
}
STARFIELD starfield;
----------------------------------------------------------------------------
--------------------------------------------------------------------------
//then I would have to make a new "COORDINATE" class for the STARFIELD class
where all references to "coordinate" is exchanged with
"starfield->star_coordinates" and this would be very bad code.
How do I solve this?
- Raw text -