如何向创建对象的类发送消息 [英] How to send a message to the class that created the object
问题描述
我想将数据发送回创建此对象的类。
与游戏有关。
敌人的对象具有线程功能,可以在场景中自行移动。
如果包含来自该类的头文件,则会产生很多错误。
敌人类:
类敌人
{
private:
void(* iChange)(DWORD&);
}:
Enemy :: Enemy(void(* iChangeHandler)(DWORD&)):iChange(0)
{
this-> iChange = iChangeHandler;
}
void Enemy :: Draw(D3DGraphics&gfx)
{
this-> iChange(this-> dwThreadID); //向创建我
gfx的类发送一条消息。PutPixel(this-> my_position_x + 0,this-> my_position_y,this->红色,this->蓝色,此->绿色);
this-> grafix-> DrawCircle(this-> my_position_x + 0,this-> my_position_y,this->半径,this->红色,this->蓝色,this->绿色);
(sprintf)(this-> enemy_buffer, X:%d,Y:%d,this-> my_position_x,this-> my_position_y);
this-> grafix-> DrawString(this-> enemy_buffer,this-> my_position_x,this-> my_position_y,& fixedSys,D3DCOLOR_XRGB(255,0,0));
}
游戏类:
struct stranger_array_ARRAY {
std :: string name;
DWORD ID;
敌人*敌人;
}敌人数组[25];
void Game :: EnemyEvent(DWORD& thread_id)
{
friends_array [0] ...... //我想访问此结构数组
}
Game :: Game(HWND hWnd)
{
friends_array [0] .name = john Doe;
friends_array [0] .ID = NULL;
nobodys_array [0] .enemy =新敌人(& Game :: EnemyEvent);
//错误:C2664:
//另一个临时
friends_array [0] .name = john Doe;
friends_array [0] .ID = NULL;
nobodys_array [0] .enemy =新敌人(Game :: EnemyEvent);
//错误C3867:
}
如果我理解正确,您想在 Game
对象上调用一个函数。这意味着您需要将指针传递给 Game
对象,以便正确调用在其上非静态成员函数指针 (iChange)。 p>
进行如下所示的更改,您应该可以做您想做的事
enemies_array [0] .enemy =新敌人(this,& Game :: EnemyEvent);
typedef void(Game :: * ChangeFunc)(DWORD&)
类敌人
{
private:
ChangeFunc iChange;
游戏* pGame;
}:
敌人(Game * pCreatorGame,ChangeFunc iChangeHandler)
{
iChange = iChangeHandler;
pGame = pCreatorGame;
}
void Enemy :: Draw(D3DGraphics& gfx)
{
(pGame-> * iChange)(this-> dwThreadID);
I would like to send back data to class that create this object.
It's game related. The enemy objects have a threaded function and move on their own in the scene.
It generates lots of errors if you include the header file from the class that creates to the objects into the object itself ... to pass pointers.
Enemy Class:
Class Enemy
{
private:
void (*iChange)(DWORD &);
}:
Enemy::Enemy(void (*iChangeHandler)(DWORD &) ) : iChange(0)
{
this->iChange = iChangeHandler;
}
void Enemy::Draw(D3DGraphics& gfx)
{
this->iChange(this->dwThreadID); // send a message back to the class that created me
gfx.PutPixel(this->my_position_x + 0,this->my_position_y,this->red,this->blue,this->green);
this->grafix->DrawCircle(this->my_position_x + 0,this->my_position_y, this->radius, this->red,this->blue,this->green);
(sprintf)( this->enemy_buffer, "X: %d, Y: %d", this->my_position_x , this->my_position_y);
this->grafix->DrawString( this->enemy_buffer, this->my_position_x , this->my_position_y, &fixedSys, D3DCOLOR_XRGB(255, 0, 0) );
}
Game Class:
struct enemies_array_ARRAY {
std::string name;
DWORD ID;
Enemy* enemy;
} enemies_array[25];
void Game::EnemyEvent(DWORD &thread_id)
{
enemies_array[0]...... // i want to acces this struct array
}
Game::Game(HWND hWnd)
{
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(&Game::EnemyEvent);
// error: C2664:
// another attemp
enemies_array[0].name = "john Doe";
enemies_array[0].ID = NULL;
enemies_array[0].enemy = new Enemy(Game::EnemyEvent);
// error C3867:
}
If I understand correctly, you want to call a function on the Game
object. This means you need to pass a pointer to the Game
object in order to correctly call a non static member function pointer(iChange) on it.
Make the changes shown below and you should be able to do what you want
enemies_array[0].enemy = new Enemy(this,&Game::EnemyEvent);
typedef void (Game::*ChangeFunc)(DWORD &)
Class Enemy
{
private:
ChangeFunc iChange;
Game *pGame;
}:
Enemy(Game *pCreatorGame, ChangeFunc iChangeHandler )
{
iChange = iChangeHandler;
pGame = pCreatorGame;
}
void Enemy::Draw(D3DGraphics& gfx)
{
(pGame->*iChange)(this->dwThreadID);
这篇关于如何向创建对象的类发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!