Hi, straight to the point: I have a good working linked list that I want a bunch of structs to inherit. Using "struct blah : Object {code};" doesn't work and I know I could get the same thing by copying the code and pasting it into all my structs and renaming parts but that would be a whole lot of copypasta and a big mess... I've read a lot of stuff on inheritance and other stuff and have learnt everything I know about c++ from internet resources, and I've been working on this linked list business for ages and I finally decided to just join a random forum and ask for help (the whole thing would have been a whole damned lot quicker if I just asked in the first place but I guess I like to struggle at things on my own).
Here's the linked list stuff, I want a way for a regular struct to inherit it:
Code:typedef struct Object{//an "object" of the list
Object *Next;
Object *Previous;
void Add();
void Remove(Object *n);
void CleanList();
} Object;
Object *First;
Object *Last;
Object *ListPointer;
void Object::Add(){
Object *Temp;
if (First){// If the list already has a First (the list exists)
Temp = new Object; //NULL <-- |First|...|Last| --> NULL
Last->Next = Temp; //NULL <-- |First|...|Last| --> |temp|
Temp->Previous = Last; //NULL <-- |First|...|Last| <-> |temp|
Temp->Next = NULL; //NULL <-- |First|...|Last| <-> |temp| --> NULL
Last = Temp; //NULL <-- |First|...|oldL| <-> |Last| --> NULL
ListPointer = Last; //NULL <-- |First|...|Last| --> NULL //and last is pointed to by the list
Temp = NULL;
delete Temp;
}
else{//there is no list yet
ListPointer = new Object;
First = ListPointer;
Last = ListPointer;
ListPointer->Next = NULL;
ListPointer->Previous = NULL;// NULL <-- |newPA| --> NULL
First = ListPointer;
Last = ListPointer;// NULL <-- |new/First/Last| --> NULL
}
};
void Object::Remove(Object *n){
if (n != First){
if (n->Next){
Object *tempb;
tempb = new Object;
tempb->Previous = n->Previous;
delete tempb;
}
if (Last == n){
Last = n->Previous;
}
}
if (n != Last){
if (n->Previous){
Object *tempc;
tempc = new Object;
tempc->Next = n->Next;
delete tempc;
}
if (First == n){
First = n->Next;
}
}
n->Next = NULL;
n->Previous = NULL;
};
void Object::CleanList(){ // Destroys the entire list
Object *tempa, *r;
r = new Object;
tempa = new Object;
r = First; // Start at the First Object of this list, for better speed
while (r){ // While r still means something
tempa = r->Next; // Save our next APointer
r->Remove(r); // Remove the one we're at
r = tempa; // And advance.
}
delete r;
delete tempa;
delete ListPointer;
First = NULL;
Last = NULL;
};