64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
class GameCharacter4;
int defaultHealthCalc2( const GameCharacter4& gc ){}
class GameCharacter4{
public:
// HealthCalcFunc is any callable entity that can be called with
// anything compatible with a GameCharacter and that returns anything
// compatible with an int:
typedef boost::function < int ( const GameCharacter4& )> HealthCalcFunc;
explicit GameCharacter4( HealthCalcFunc hcf = defaultHealthCalc2 )
:healthFunc( hcf )
{}
int healthValue() const
{ return healthFunc( *this ); }
private:
HealthCalcFunc healthFunc;
};
class GameLevel{
public:
int health( const GameCharacter4& ) const // health calculation
{} // member function; note
// non-int return type
static int health2( const GameCharacter4& )
// health calculation using static member function
{}
};
class EvilBadGuy4 :public GameCharacter4{};
int main()
{
GameLevel currentLevel;
EvilBadGuy4 ebg2( boost::bind( &GameLevel::health, currentLevel,_1 )); // why cant
EvilBadGuy4 ebg3( &GameLevel::health2 ); // ??? Why can't it work
return 0;
}