标签:style blog http color 使用 io
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
1.virtual 函数版本
class GameCharacter{ public: virtual int healthValue() const; //返回人物的健康指数, derived classes 可重新定义它 };
class GameCharacter{ public: int healthValue() const // Item 36 derived classes不重新定义它 { //... int retVal = doHealthValue(); //... return retVal; } private: virtual int doHealthValue() const //derive classes 可重新定义它 { //... 缺省算法 } };
//1.藉由 Function Pointers 实现 Strategy 模式 int defaultHealthCalc(const GameCharacter &gc);<span style="font-family: Arial, Helvetica, sans-serif;">//计算健康指数的缺省算法</span> class GameCharacter{ public: typedef int (*HealthCalcFunc)(const GameCharacter); explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc) : healthFunc(hcf){} int healthValue() const{ return healthFunc(*this); } //... private: HealthCalcFunc healthFunc; };
//2.藉由 tr1::function 完成 Strategy 模式 //以 tr1::function 成员变量替换 virtual 函数,因而允许使用任何可调用物(callable entity) 搭配一个兼容于需求的签名式。这也是 Strategy 设计模式的某种形式。 class GameCharacter; int defaultHealthCalc(const GameCharacter &gc); class GameCharacter{ public: //HealthCalcFunc 可以是任何“可调用物”,可被调用并被接受任何兼容于 GameCharacter 之物,返回任何兼容于 int的东西 typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc; explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc) : healthFunc(hcf){} int healthValue() const{ return healthFunc(*this); } //... private: HealthCalcFunc healthFunc; };
class GameCharacter; class HealthCalcFunc{ public: //... virtual int calc(const GameCharacter &gc) const{...} }; HealthCalcFunc defaultHealthCalc; class GameCharacter{ public: explicit GameCharacter(HealthCalcFunc *phcf = &defaultHealthCalc) : pHealthCalc(phhcf){} int healthValue() const {return pHealthCalc->calc(*this);} private: HealthCalcFunc *pHealthCalc; };
Effective C++ Item 35 考虑 virtual 函数以外的实现,布布扣,bubuko.com
Effective C++ Item 35 考虑 virtual 函数以外的实现
标签:style blog http color 使用 io
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37807181