class Repository
{
private:
MyArray<Something> *myList;
MyArray<MyArray<Something>> *undo;
public:
// constructor, destructor, append, sort, undo last action, no point in writing them
}
Now I need to provide a way for the user to chose if the program will use files to save the lists or just keep it like this in memory.
I can write a new class, RepositoryInFile that will extend the Repository class
1 2 3 4 5 6 7 8 9 10 11
class RepositoryInFile: public Repository
private:
void readFromFile();
void writeToFile();
public:
// again, no point in writing each function, just one to make my idea clear
void append(Something newElement)
{
Repository::append(newElement);
writeToFile();
}
but if I do something like
1 2
Repository *r = new RepositoryInFile();
r.append(x);
the append from Repository will be used, not the one I want (the one from RepositoryInFile)
Do I need to make a entire new class, let's say VirtualRepository, that will have virtual methods for everything I need and then just make both Repository and RepositoryInFile extend that class?
Is there a quicker (and maybe more elegant) way of doing this?
So just make them virtual in Repository and make RepositoryInFile extend it?
That means that in headers I'll have
1 2 3 4 5
class Repository
{
virtualvoid append(Something newElement);
// everything like this
}
and my implementation of the Repository class will stay unchanged from what I have now?
That will make my life a lot easier as I'll have to do less changed to my actual code.