This is a possible implementation
template<typename View, typename Model>
struct Controller {
View *view;
Model *model;
Controller() : view(0), model(0) {}
void setUp(View *v, Model *m) {
view = v;
model = m;
}
virtual void change() = 0;
};
template<typename Controller, typename Model>
struct View {
Controller *controller;
Model *model;
View() : controller(0), model(0) {}
void setUp(Controller *c, Model *m) {
controller = &c;
model = &m;
}
virtual void display() = 0;
};
To do the instantiation the trick is deriving a class from a template that has a forward declared class as parameter:
struct MyModel {
int x;
MyModel(int x) : x(x) {}
};
struct MyController;
struct MyView : View<MyController, MyModel>
{
void display() { std::cout << model->x << std::endl; }
};
struct MyController : Controller<MyView, MyModel>
{
void change() { model->x = 44; }
};
After that you can create instances and set them up
int main(int argc, const char *argv[]) {
MyModel m(42);
MyView v;
MyController c;
v.setUp(&c, &m); c.setUp(&v, &m);
v.display();
c.change();
v.display();
return 0;
}