I am trying to implement cocoa's delegation pattern in c++.
I have tried to emulate the cocoa delegation pattern in imagepicker example as given below. However, I am not sure this is the correct way of implementing in C++, and wonder if somebody has an idea for something better. I also notice that this implementation I came out is nothing to do with traditional c++ delegate(?) ( see here ) . I am not even sure delegate in the previous link is actually delegate pattern (I think it is nothing but function pointer).
class ImagePickerControllerDelegate { // defined as protocol in swift
public:
virtual void ImagePickerFinished() = 0;
};
class ImagePickerController {
public:
ImagePickerControllerDelegate * delegate;
private:
void findImageDirectory() {}
void checkUserPermission() {}
void loadImage(char* image_name) {}
void andDoOtherThings() {}
public:
void Run() {
// doing long stuff here ..
findImageDirectory();
checkUserPermission();
loadImage("lena");
andDoOtherThings();
// done, notify
delegate->ImagePickerFinished();
// other clean-up etc.
}
};
class MainViewController: ImagePickerControllerDelegate {
ImagePickerController ImagePicker = ImagePickerController();
public:
MainViewController() {
this->ImagePicker.delegate = this;
}
void UserClicked() {
ImagePicker.Run();
}
virtual void ImagePickerFinished() {
std::cout << "image picker finished..";
}
};
In short I need good suggestions for simple and beautiful implementation of cocoa delegate pattern in C++. I really do not like ugly STL template stuff, so please do not give suggestions/examples that uses it.