I'm trying to implement a Strategy design pattern in C++.
I have an abstract class with no non-abstract method called ICookingStrategy:
class ICookingStrategy
{
public:
virtual int cook() = 0;
};
From this abstract class inherit two child classes: fastCooking and slowCooking:
Child classes
class fastCooking : public ICookingStrategy
{
public:
int cook() { return 0; }
};
class slowCooking : public ICookingStrategy
{
public:
int cook() { return 0; }
};
Context class
The point is, I want another class to be populated by an object instanciating either fastCooking or slowCooking, indifferently. Which means, an object instantiating ICookingStrategy.
But my problem is: I can't find how I can populate this receiving class. I thought in the constructor's arguments, but when I try this, g++ gives me an error:
class CookingContext
{
public:
CookingContext (ICookingStrategy* cookingStrategy):cookingStrategy(cookingStrategy)
{}
int cook()
{
this->cookingStrategy.cook();
return 0;
}
private:
ICookingStrategy* cookingStrategy;
};
In main()
, I want to do instantiate one of the two child classes, then populate CookingContext
like this:
int main(void)
{
ICookingStrategy* cookingStrategy;
cookingStrategy = new fastCooking;
CookingContext cooking(cookingStrategy);
cooking.cook();
return 0;
}
G++ error message on compilation is:
error: expected ‘)’ before ‘*’ token
CookingContext(ICookingStrategy* cookingMode):cookingMode(cookingMode)
^
So my question is...
What is the best way to make CookingContext
being populated by the strategy
object, instantiating either child of ICookingStrategy
?
(tried many different ones, and didn't manage to make it work)