Sign in to follow this  
sheep19

C++ function pointer

Recommended Posts

Let's say that I have a this function:
bool FrameFunc();

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
....

How can I create the same thing inside a class? I tried:
Game::Game(HINSTANCE, HINSTANCE, LPSTR, int)
:hge(NULL), manager(NULL)
{
	// open file
	std::ifstream in("settings.txt");

	hge = hgeCreate(HGE_VERSION);

	hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);

(FrameFunc is a member function of Game) I get: 'Game::FrameFunc': function call missing argument list; use '&Game::FrameFunc' to create a pointer to member Why? there is no argument list..

Share this post


Link to post
Share on other sites
This comes up on the forums once every few weeks or so. Rather than write up a long, detailed reply, I'll instead just refer you to the forum archives, as you'll find a lot of detail on the subject there and a lot of suggestions as to how to work around it (where it's possible to do so, at least).

The phrase to search for would probably be 'C++ member function pointers'. If you have trouble finding the info you're looking for, post back and we can probably direct you towards some relevant threads.

Share this post


Link to post
Share on other sites
I have taken a look at C++ FAQ lite which I found using the search facility of the website (thanks jyk for not telling the answer but rather letting me find it!).

So, it says that in order to do that, the function has to be static. So I made it static. I also made the variables static (static member functions work with static data members). The class has become this:

interface

class Game
{
static HGE *hge;

static hgeResourceManager *manager; // pointer to an hgeResourceManager object

public:

// constructor
Game(HINSTANCE, HINSTANCE, LPSTR, int);

// destructor
~Game();

static bool FrameFunc();
};

#endif // GAME_H



implementation

#include "Game.h"

HGE *Game::hge = 0;
hgeResourceManager *Game::manager = 0;

Game::Game(HINSTANCE, HINSTANCE, LPSTR, int)
{
hge = hgeCreate(HGE_VERSION);

hge->System_SetState(HGE_FRAMEFUNC, Game::FrameFunc);

manager = new hgeResourceManager("resource.res");
}

Game::~Game()
{
delete manager;

// these lines have to be the last
hge->System_Shutdown();
hge->Release();
}

bool Game::FrameFunc()
{
return true;
}



It works now. I have thought of making the class a singleton or keep a static bool inside the class to check whether it has been initialized or not. The bool will be set to true the first time. So when I do this: Game g; Game g2; g2 won't do anything ('cause the data members will be static, so calling g2 and g will be same)

I have also thought of the namespace solution like CadetUmfer stated.


namespace Game
{
HGE *hge;

class Init
{
// class used to initialize and destroy hge in its constructor and destructor automatically
};

// functions
bool RenderFunc();

/*.....*/
}



I think I prefer the first solution. Are there any things I should consider?

Share this post


Link to post
Share on other sites
Just a tip, when you delete a pointer, you should set it to NULL or 0. That way it doesn't continue to appear as a valid pointer. Also removes issues with deleting it a second time on accident (as delete checks for null values.)

That solution is decent enough, but a more suitable approach (in my opinion) would be something along the lines of:


class Game;
// define your Game class

Game MyGame;

bool Frame();

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HGE *Engine = hgeCreate(HGE_VERSION);
if(Engine == 0)
return 0; // You'd probably wan't to notify the user before just exiting.
Engine->System_SetState(HGE_FRAMEFUNC);

// You'll more than likely want Game::SetEngine/SetHGE/etc so that Game has a copy of the HGE pointer.
/* the rest of the code here */

Engine->System_Shutdown();
Engine->Release();
return 0;
}

bool Frame()
{
return MyGame.Frame();
}





HTH!

Donald Beals

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this