Imagine this simple base class:
struct simple_http_service
{
virtual reply http_get(…);
virtual reply http_post(…);
virtual reply http_delete(…);
// etc.
};
I’d like to prevent the user from deriving from this class without overriding at least one of these and prevent them from instantiang simple_http_service
Is there some nice way to do this?
That sounds like a really odd constraint. By all means protect the user from incorrect usage, but don’t try to prohibit things that you just “can’t see the point of”. If there’s no point in deriving from your class without overriding any of the three functions, then let the user override as many or as few function as he likes, and trust that he won’t do the pointless thing of deriving without overriding any of the functions. There’s no harm in the user doing that, it’s just not very useful.
But if you do need to enforce this (again, I’d suggest you rethink), then don’t use virtual functions. Instead, pass function pointers or function objects (or
std::function/boost::function) callbacks. Make the base class look something like this:Now just add the necessary constructors (or free/static functions so you can name them to avoid ambiguity) so that the class can only be instantiated when at least one of the function objects are supplied.