In effective c++, item 35, the author introduces the strategy pattern via function pointers. Specifically on page 172
class GameCharacter;
int defaultHealthCalc(const GameCharacter& gc);
class GameCharacter {
public:
typedef int (*HealthCalcFunc)(const GameCharacter&);
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)//why not &defaultHealthCalc?
: healthFunc(hcf)
{}
int healthValue() const
{ return healthFunc(*this); }
...
private:
HealthCalcFunc healthFunc;
};
On the sixth line, why the assignment to the function pointer HealthCalcFunc
is defaultHealthCalc
instead of &defaultHealthCalc
?