I have a service class which later will be used for storing multiple implementations in one container to call Process
on all of them
struct Service
{
virtual ~Service();
virtual void Process() = 0;
};
There are two services which share common logic, so instead of copying code i've created common base class which i forbid to create manually by making constructor protected
struct CommonServiceLogic : Service
{
protected:
CommonServiceLogic();
void Process() final override {
/*do smth common*/
ProcessImpl();
}
virtual void ProcessImpl();
};
Now i can define my services next way
struct EasyService : CommonServiceLogic {
using CommonServiceLogic::ProcessImpl;
};
struct ComplicatedService : CommonServiceLogic {
void ProcessImpl() final override {
/*do smth special*/
CommonServiceLogic::ProcessImpl();
}
};
Questions:
EasyService::ProcessImpl
as final? I can do it by redefining a method, but is there some language way?Reason for marking this method as final is pretty simple: i want to show (to compiler or to next programmer) that this override is the last one. using
allows to create a child from EasyService
with redefinition of ProcessImpl
.
Note that i cannot finalize ComplicatedService
and EasyService
for reasons which is hard to explain
Service::Process
will be called in the place which does not know about EasyService
nor ComplicatedService
If I understand your question, then you are asking whether you can apply final
to a function from the superclass that was imported using using
. This does not seem to be possible unless you redefine the virtual function in EasyService
, so your fix seems like the correct option.