Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (AndrewAMD, Ayumi, NewbieZorro), 14,141 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Passing function pointers with parameters #229816
09/30/08 18:25
09/30/08 18:25
Joined: May 2008
Posts: 123
B
BES Offline OP
Member
BES  Offline OP
Member
B

Joined: May 2008
Posts: 123
Hey all!

I define a function pointer (globally defined):
void myFunc( myStruct* ptrStruct );

and give it a default definition:
void myFunc( myStruct* ptrStruct )
{
diag("\nOriginal function called, override this!");
}

Now the function I want to actually call:
void myDesiredFunc( myStruct* ptrStruct )
{
diag("\nCorrect function called");
}

Here's where the issue comes in- I need to set my function pointer in an intermediary function that is part of the API I'm building:
void SetMyFunction( function* newfunc )
{
myFunc = (function*)newfunc;
}

myStruct myStructInstance;
in main()
{
...
SetMyFunction( myDesiredFunc );
myFunc( &myStructInstance );
...
}

But the original function definition is used.
The function pointer (myFunc) and structure pointer parameter are globally defined. I've tried several casts in the SetMyFunction function (i.e. function*, function, void*, etc.)
Anyone see what I'm missing? Do I need to somehow specify the parameter type in my cast; something like
myFunc = (function(myStruct*))newfunc; //??

Thanks so much for your time!

Re: Passing function pointers with parameters [Re: BES] #229824
09/30/08 19:32
09/30/08 19:32
Joined: May 2008
Posts: 123
B
BES Offline OP
Member
BES  Offline OP
Member
B

Joined: May 2008
Posts: 123
Alright, got it:

void SetMyFunction( void* newfunc )
{
myFunc = (function*)newfunc;
}

The problem was using the same name for myFunc and the default definition. To get around this I just say:
void myFunc( myStruct* ptrStruct );

and

void defaultFunc( myStruct* ptrStruct );
{
diag("\nDefault function called!");
}

then in main:

myFunc = defaultFunc

Then I'm able to set myFunc to myDesiredFunc later on.

Hope this helps someone in the future! Cheers!


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1