First, I know the answer is somewhere out there, but I've been searching all morning and didn't find it.
My question concerns what in Java looks like
abstract class AbstractWorker {
public abstract int doIt();
}
class Thinker extends AbstractWorker {
public int doIt() { return 42; }
}
class Milkmaid extends AbstractWorker {
public int doIt() { return 1 + 2; }
}
class Worker {
public int getWorkDone(AbstractWorker worker) {
return worker.doIt();
}
}
I think this should be possible in C++, too. But how do I implement this? My approach would look like
struct AbstractWorker {
virtual int doIt() = 0;
};
struct Thinker : public AbstractWorker {
int doIt() { return 42; }
};
struct Milkmaid : public AbstractWorker {
int doIt() { return 1 + 2; }
};
struct Worker {
int getWorkDone(AbstractWorker &worker) {
return worker.doIt();
}
};
What's wrong with this? Or how would you solve this?