gpt4 book ai didi

c++ - 函数指针的赋值(effective c++ item 35)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:33 25 4
gpt4 key购买 nike

<分区>

effective c++, item 35中,作者通过函数指针引入了策略模式。具体在第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;
};

第六行,为什么函数指针HealthCalcFunc的赋值是defaultHealthCalc而不是&defaultHealthCalc

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com