Call a C++ base class method automatically

后端 未结 5 1750

I\'m trying to implement the command design pattern, but I\'m stumbling accross a conceptual problem. Let\'s say you have a base class and a few subclasses like in the examp

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 11:36

    Split the operator in two different methods, e.g. execute and executeImpl (to be honest, I don't really like the () operator). Make Command::execute non-virtual, and Command::executeImpl pure virtual, then let Command::execute perform the registration, then call it executeImpl, like this:

    class Command
       {
       public:
          ResultType execute()
             {
             ... // do registration
             return executeImpl();
             }
       protected:
          virtual ResultType executeImpl() = 0;
       };
    
    class SomeCommand
       {
       protected:
          virtual ResultType executeImpl();
       };
    

提交回复
热议问题