Hi,
I have 2 main classes: BookShelf and Library. Both these classes need to have a member class which would be BookList. Now BookList is a linked list with each object have listing of 10 books and a link pointer to another BookList. I need to maintain a list pointer for the Shelf objects separately and the Library separately. So I have a bookIndex variable for both of them. But since both of them are different classes how do I set the definition of parent in BookList class so that its getNextBook function is the same. I have pasted simplified code below:
struct Book{
std::string BookName;
std::string Author;
}
class BookList{
public:
class BookList *prevList;
class BookList *nextList;
struct Book thisList[10];
class <ParentType> *parent;
class Book *getNextBook(){
parent->bookIndex = parent->bookIndex + 1;
if(parent->bookIndex == 10){
parent->bookIndex = 0;
return nextList->thisList[0];
}
else {
return thisList[parent->bookIndex];
}
}
}
class BookShelf{
public:
int bookIndex;
class BookList shelfList;
}
class Library{
public:
int bookIndex;
class BookList libList;
}
After thinking about the solution I came to realize that if I define a class Something then each instance of BookList has that. My purpose of putting bookIndex in the parent was to have one index for all the linked lists (forming one big list) for one object that being a BookShelf or Library.
If I put bookIndex inside BookList then I am using more memory to store all the extra bookIndex variables.
So is there any other memory efficient way rather other than defining 2 separate BookList classes?