In following example notice the TERMINATOR which is instance of AbstractMiddleware:
#include <iostream>
class AbstractMiddleware { // base class for middleware
public:
AbstractMiddleware();
virtual ~AbstractMiddleware() { }
virtual void call() = 0; // typical middleware does something and calls next one in chain
AbstractMiddleware* next;
};
static class : public AbstractMiddleware {
public:
void call() override {
std::cout << "TERMINATE" << std::endl;
}
} TERMINATOR; // dummy middleware to terminate the chain
AbstractMiddleware::AbstractMiddleware():
next(&TERMINATOR) // each middleware is terminated by default
{ }
I don't like that TERMINATOR is declared outside of AbstractMiddleware. In fact, it's the only place where TERMINATOR is used (for now). Ideally I would like to hide it within AbstractMiddleware as a static field AbstractMiddleware::TERMINATOR, but don't know how.
[EDIT]
It turns out that I was not clear enough in my original question.
As correctly guessed by StoryTeller and Dialecticus, my initial concern was about hiding TERMINATOR instance from common namespace while keeping it visible to AbstractMiddleware and its descendants.
I thought that it will be possible to put it into AbstractMiddleware as static like it's done here: https://stackoverflow.com/a/21197907/947418 But it turns out that it doesn't work in case of abstract classes.