I have a class RAW_FILE which is the base class, and a class DIRECTORY which is derived from RAW_FILE.
The code below shows my problem, and I would like to know if there is a way to solve it without just creating a virtual method (printToScreen) in the base class.
In this code, I cannot assign the address held in temp_dir->next (RAW_FILE* next) to temp_dir (DIRECTORY* temp_dir).
Note also that first_subdir is declared as 'DIRECTORY* first_subdir;'
Code:
unsigned int DIRECTORY::printToScreen(bool print_directories = false) {
...
DIRECTORY *temp_dir = first_subdir;
while(temp_dir != NULL) {
counter = counter + temp_dir->printToScreen(print_directories);
temp_dir = temp_dir->next;
// 'next' is a RAW_FILE pointer, so this causes a compile error when trying to
// assign it to temp_dir (DIRECTORY POINTER)
}
}
But if, I make temp_dir a RAW_FILE pointer, I cannot call the method printToScreen, because the method doesn't exist in RAW_FILE objects.
Code:
unsigned int DIRECTORY::printToScreen(bool print_directories = false) {
...
RAW_FILE *temp_dir = first_subdir;
while(temp_dir != NULL) {
counter = counter + temp_dir->printToScreen(print_directories);
//printToScreen is a method in the DIRECTORY class, so can't be called from
//the base class RAW_FILE
temp_dir = temp_dir->next;
}
}
Code:
class RAW_FILE {
protected:
char* path;
public:
RAW_FILE *previous, *next;
};
class DIRECTORY: public RAW_FILE {
private:
DIRECTORY *first_subdir, *last_subdir;
RAW_FILE *first_file, *last_file;
public:
unsigned int printToScreen(bool);
};