I am trying to implement a simple file system. I have a base class entry. I have two classes that inherit from entry called File and Directory. Directory contains a list of Entry objects. I have a method called changeDir for Directory and File but not Entry. I was hoping I could call this method on Entry objects and the compiler would know which method to use based on whether the Entry was a Directory or File. Any way to accomplish this?
class Entry{
public:
std::chrono::time_point<std::chrono::system_clock> dateCreated;
std::string fileName;
Entry(std::string name);
~Entry();
};
Directory* Directory::changeDir(Directory* dir, Directory* currentDir){
currentDir = dir;
return currentDir;
}
void File::changeDir(Directory* dir, Directory* currentDir){
std::cout<<"You cannot change directories into a file\n";
}
unordered_set<Entry*> s;
s.add(File);
s.add(Directory);
for each in s: each.changeDir(); //pseudo code
Unfortunately it tells me a changeDir() method has not been declared for Entry class. I would normally just make a method for the Entry class but it says Directory is not declared in this scope (obviously, since Directory is a derived class of Entry). I feel like I've done this type of polymorphism in python before. Is there no way to do in C++? Thanks.
Edit: Set takes pointers to Entry objects, not actual Entry objects.
Edit2: It seems that virtual classes is the solution to this specific issue. I believe the better and more robust solution will be to have each directory contain a set of files and a set of directorys instead of one set of both.