0

我想将数据发送回创建此对象的类。

是游戏相关的。敌人物体具有线程功能,并在场景中自行移动。

如果将创建对象的类中的头文件包含到对象本身中,则会产生很多错误……以传递指针。

敌人等级:

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

游戏类:

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: 
}
4

1 回答 1

1

如果我理解正确,您想在Game对象上调用一个函数。这意味着您需要传递一个指向Game对象的指针,以便在其上正确调用非静态成员函数指针(iChange)。

进行如下所示的更改,您应该能够做您想做的事

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);
于 2012-12-09T12:04:25.910 回答