Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Toris

Function Pointers

This topic is 5423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying out some new ideas on how to structure my programs, basically, the idea is this: The main loop calls a function, constantly, Process();, which will point to the correct event processor, depending on what's happening, menu showing, in-game, whatever. Each of the things, menu, in-game, have their own class, and some common variables etc..., which is in cBase, and they inherit that, now whenever the process changes, from the menu to in-game for example, I just update the Process(); function to point to the new function, and it continues as normal. However, I'm having a little problem with the code to do this. This is the code I'm using
cGame *gameClass = new cGame;
void (*Process)( void );
Process = &gameClass->ProcessLoop;
 
however, it gives the following compile time error:
Error   : illegal implicit conversion from 'void (*)()' to
'void (*)()'
GlooP.cpp line 74    Process = &gameClass->ProcessLoop;  
 
I have tried doing something like:
void (cBase::*Process)( void );
 
(where cBase is the base class cGame, cMainMenu and cLevelEditor inherit) - but that just gives different errors. Any ideas, things you can point me to to read, etc...? Many thanks, -Toris [edited by - Toris on January 11, 2004 7:39:26 PM]

Share this post


Link to post
Share on other sites
Advertisement

class Class
{
public:
void MemberFunction(void);
};

Class Object;
class *ObjectPointer = new Class;
void (Class::*MemberFunctionPointer)(void);

MemberFunctionPointer = &Class::MemberFunction;

Object.*MemberFunctionPointer();
ObjectPointer->*MemberFunctionPointer();


The last two statements are calls to bound pointers to members. Bound pointers to members cannot be manipulated in any other way. Keep the object and the pointer separate until it is time to make the call. There are no other legal ways to do it. Pointers to members are not function pointers. They cannot be passed to APIs that expect regular function pointers. Even in unbound form.

Use boost::‍bind and boost::function for a convenient workaround.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
I did think from the errors I was getting, that it might not be possible to do what I''m doing. But I was sort of hoping I was wrong

What would you imagine to be the best way to do what I outlined in the first post?

If worst comes to worst, I guess I could always do something simple like:

cGame gameClass;
cMainMenu menuClass;

const int GAME_STATUS = 0;
const int MENU_STATUS = 1;

int gameStats = GAME_STATUS;

while( true ) {
if( gameStatus == GAME_STATUS ) {
gameClass->ProcessLoop();
} else if( gameStatus == MENU_STATUS ) {
menuClass->ProcessLoop();
}
}

Share this post


Link to post
Share on other sites

class Handler
{
public:
virtual void Execute() = 0;
virtual void ~Handler() {}
};

class Game : public Handler
{
public:
virtual void Execute() { /* ... */ }
};

class Menu : public Handler
{
public:
virtual void Execute() { /* ... */ }
}

bool running = 1;
int GameStatus = 0;
Handler* Handlers[2];

int main()
{
Handlers[0] = new Game;
Handlers[1] = new Menu;

while(running) Handler[GameStatus]->Execute();

delete Handlers[0];
delete Handlers[1];
}


std::vector, boost::shared_ptr, boost::function and boost::‍bind are all tools that may prove useful here.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on January 11, 2004 8:29:59 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!