0

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: 
}
jrouquie
  • 4,315
  • 4
  • 27
  • 43
NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • A class is just a type. You can't send anything to a type. Can you send a message to an int? – Kerrek SB Dec 09 '12 at 12:00
  • i'm trying to achieve what you can do with c# ... callbacks and i have read lots and lots over it .. but none that i realy interpret as simular to c# – NaturalDemon Dec 09 '12 at 12:02
  • if the `EnemyEvent(DWORD &thread_id)` is static ... the code works, but i can't acces the variables in/from the game class – NaturalDemon Dec 09 '12 at 12:04

1 Answers1

1

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);
Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • but i can't `#include "Game.h"` into the Enemy class ... otherwise those indeed would be possible ... i think – NaturalDemon Dec 09 '12 at 12:09
  • You need only pointer to game in the header - just use forward declaration `class Game` above `class Enemy{` – Karthik T Dec 09 '12 at 12:09
  • Similarly, `Game.h` does not need to include `Enemy.h` just `class Enemy` at the top should do, since you need only a pointer to enemy. – Karthik T Dec 09 '12 at 12:11
  • hmmm ... 1>c:\..\game.cpp(78): error C2664: 'Enemy::Enemy(const MouseServer &,int &,int &,Game *,void (__cdecl &)(DWORD &))' : cannot convert parameter 5 from 'void (__thiscall Game::* )(DWORD &)' to 'void (__cdecl &)(DWORD &)' ` i did what you told me – NaturalDemon Dec 09 '12 at 12:22
  • I forgot to change the type of the function pointer to indicate it was a member function, fixed it. I have also made the function signature into a `typedef` easier to read/write the code now. – Karthik T Dec 09 '12 at 12:26
  • `(pGame->*iChange)(this->dwThreadID);` error C2297: '->*' : illegal, right operand has type 'void (__cdecl *)(DWORD &)' ... hard material – NaturalDemon Dec 09 '12 at 12:31
  • thank you, it works ... this was the harder stuff i encountered during my short c++ carriere ... thnx again : ) – NaturalDemon Dec 09 '12 at 12:40
  • You are welcome! Non static member function pointer is definitely not an easy concept – Karthik T Dec 09 '12 at 13:35