Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement strategy pattern in C++ with std::function

Tags:

I'm reasing about the best way to implement the strategy pattern in C++. Up to now, I've always used the standard way, where the context has a pointer to the base strategy class as follows:

 class AbstractStrategy{
 public:
     virtual void exec() = 0;
 }
 class ConcreteStrategyA{
 public:
     void exec();
 }
 class ConcreteStrategyB{
 public:
     void exec();
 }

 class Context{
 public:
     Context(AbstractStrategy* strategy):strategy_(strategy){}
     ~Context(){
          delete strategy;
       }
      void run(){
           strategy->exec();
      }
 private:
     AbstractStrategy* strategy_;

Since having pointers to objects can result in bad behavior, I was looking for a safer way to implement this pattern and I found this question where std::function are proposed as a better way to handle this pattern.

Could someone please explain better how std::function works, maybe with an example with the strategy pattern?