While trying to learn about poco networking libraries here, I came across the following snippet:
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &)
{
return new MyRequestHandler;
}
};
I am having trouble understanding the return type of the method (HTTPRequestHandler*) and the arguments to the method(const HTTPServerRequest &).
Why is it that the return type a HTTPRequestHandler pointer? Does new MyRequestHandler return an address to an object which can be referred to by its base type?
Also, I understand const is used to make the reference immutable so that the method does not mutate the referenced object but there’s no name provided for the reference type and its not being used in the createRequestHandler method. Can somebody tell me what might be going on here?
Thanks
Return type
If you look at the “Learning Poco” code web site,
MyRequestHandler is derived from HTTPRequestHandler. So, MyRequestHandler is a HTTPRequestHandler because of inheritance. So it is valid to return a pointer to a MyRequestHandler, because it is also a pointer to a HTTPRequestHandler.
Function Argument
The snippet is confusing because it specifies an argument as a type but no variable name. It is in fact the same as:
The ‘notUsed’ variable is … not used. So you ask, why is there an argument at all? Because it is overriding a function declared in the base class HTTPRequestHandlerFactory. This function will have an argument
const HTTPServerRequest &, therefore it must also appear in the overriding function in the derived class (even though it is not used). If ‘notUsed’ were to be used in the functioncreateRequestHandler(), theconstkeyword ensures that it cannot be changed insidecreateRequestHandler().