关于function 和 bind 的问题。。。。

jibee 2008-08-25 11:22:14
为什么main 函数的两条语句不能运行?




#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;
}








...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jieao111 2008-08-25
  • 打赏
  • 举报
回复
class EvilBadGuy4 :public GameCharacter4{};
是什么意思?
还有构造函数不能继承,要在派生类构造函数中显示调用基类的构造函数
  • 打赏
  • 举报
回复
你只是构造了一个函数对象.
ebg2( 参数 );才行
ebg3( 参数 );
太乙 2008-08-25
  • 打赏
  • 举报
回复
汗~~帮顶!友情up~
realyigo 2008-08-25
  • 打赏
  • 举报
回复
try this boy.

class GameLevel{
public:
int health( const GameCharacter4& ) const // health calculation
{
return 1;
} // member function; note
// non-int return type
static int health2( const GameCharacter4& )
// health calculation using static member function
{
return 1;
}
};

class EvilBadGuy4 :public GameCharacter4
{
public:
explicit EvilBadGuy4( HealthCalcFunc hcf = defaultHealthCalc2 )
:GameCharacter4(hcf)
{}
};

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧