class Handler {
public:
Handler(): next(nullptr) {};
virtual ~Handler() {};
virtual void HandleRequest(int data) {
if (next) {
this->next->HandleRequest(data);
}
}
void SetNextHandler(Handler* next_handler) { //Здесь вроде проблема
if (next) {
this->SetNextHandler(next_handler);
} else {
this->next = next_handler;
}
}
public:
Handler* next;
};
class ConcreteHandler1 : public Handler {
public:
void HandleRequest(int data) {
//if can handle it do it
//implementation of HadleRequest
//else
std::cout << "ConcreteHandler1" << std::endl;
Handler::HandleRequest(data);
}
};
class ConcreteHandler2 : public Handler {
public:
void HandleRequest(int data) {
//if can handle it do it
//implementation of HadleRequest
//else
std::cout << "ConcreteHandler2" << std::endl;
Handler::HandleRequest(data);
}
};
int main(int argc, const char * argv[]) {
Handler* handler = new Handler;
int data;
handler->SetNextHandler(new ConcreteHandler1());
handler->SetNextHandler(new ConcreteHandler2());
handler->HandleRequest(data);
return 0;
}
void SetNextHandler(Handler* next_handler) {
if (next) {
next->SetNextHandler(next_handler); // <- next, не this
} else {
this->next = next_handler;
}
}
Handler::HandleRequest(data);
в конце каждого хендлера.