Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273222
  • 博文数量: 55
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 737
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-13 18:06
文章分类

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类: C/C++

2008-09-12 01:28:48

This basic design — having clients call private virtual functions indirectly through public non-virtual member functions — is known as the non-virtual interface (NVI) idiom
 

class GameCharacter {

public:
  int healthValue() const // derived classes do not redefine
  { // this — see Item 36
    ... // do "before" stuff — see below
    int retVal = doHealthValue(); // do the real work
    ... // do "after" stuff — see below
    return retVal;
  }
  ...
private:
  virtual int doHealthValue() const // derived classes may redefine this
  {
   ... // default algorithm for calculating
  } // character's health
};

 

we replace the use of a function pointer (such as healthFunc) with an object of type TR1::function. As explains, such objects may hold any callable entity (i.e., function pointer, function object, or member function pointer) whose signature is compatible with what is expected.

 

int defaultHealthCalc(const GameCharacter& gc); // as before

typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;

阅读(670) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~