Skip to main content
gamedev.net
🔒 Locked

Function Pointers

Started by Shannon Barber Jun 25, 2000 at 8:38 PM 23 replies 1.1k views
Original Post
Shannon Barber
Shannon Barber
I''m gonna keep a crackin at it, but if anyone has a short snippet readily availible, i would really appreiciate a function pointer class property example... The only book I have that even mentions them is LaMonthe''s "Tricks of the Window''s Game Programming Gurus" and there''s only two sentences, and 5 lines of code... not in a class...
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
ImmaGNUman
ImmaGNUman
Function pointer class property? sounds scary.

Really, the only use (i have found) for function pointers are:

1) Arrays of function pointers
2) Simulating a class in C
3) For some crazy reason you need the address of the function

Edited by - ImmaGNUman on June 25, 2000 9:51:01 PM
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
baskuenen
baskuenen
My version of a function pointer class property example...?...!

    
typedef int (*SORTPROC)(NODE *pNode, float fValue);

void Sort(TREE *pTree, SORTPROC *SortProc)
{
SortProc(pNode, fValue);
}

int NewSortProc(NODE *pNode, float fValue)
{
return pNode->fValue - fValue;
}

int Main()
{
Sort(pTree, (SORTPROC)NewSortProc);
}


Is this what you mean?

Shannon Barber
Shannon Barber
Speed in the Message Pump

I don't want to have to make an object dereferenced function call for each WM_ case... particlularily the on idle Render();

something like

CWindow* MyWindow;
CWorld* Toril;
//init
//init
//init

MyWindow->SetRenderFunc(Toril->Render);
...
Just like the GLUT messsage pump...

You send WinProc as a funtion pointer... why can't I do it?

And that example LaMonthe gives don't work!

Edited by - Magmai Kai Holmlor on June 25, 2000 9:58:12 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Shannon Barber
Shannon Barber
quote:
Original post by baskuenen

My version of a function pointer class property example...?...!

        
typedef int (*SORTPROC)(NODE *pNode, float fValue);

void Sort(TREE *pTree, SORTPROC *SortProc)
{
SortProc(pNode, fValue);
}

int NewSortProc(NODE *pNode, float fValue)
{
return pNode->fValue - fValue;
}

int Main()
{
Sort(pTree, (SORTPROC)NewSortProc);
}


Is this what you mean?





very close, thanks
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Shannon Barber
Shannon Barber
    
typedef int NODE;
typedef int TREE;
typedef int (*SORTPROC)(NODE *pNode, float fValue);
void Sort(TREE *pTree, SORTPROC *SortProc)
{
NODE* pNode;
float fValue;
SortProc(pNode, fValue);
}
int NewSortProc(NODE *pNode, float fValue)
{
return pNode->fValue - fValue;
}
int Main()
{
Sort(pTree, (SORTPROC)NewSortProc);
}

gives me a:
winmain.cpp(26) : error C2064: term does not evaluate to a function
on
SortProc(pNode, fValue);
in the Sort(TREE...) function...
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Shannon Barber
Shannon Barber
    
extern void APIENTRY glutDisplayFunc(void (*)(void));

hum
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
baskuenen
Drop the * in:

    
void Sort(TREE *pTree, SORTPROC SortProc)


Shannon Barber
Shannon Barber
Ahhh, thank you very much
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
acw83
acw83
They are required to load functions from a dynamically loaded library I think.

---
www.crazycrackerz.org
Shannon Barber
Shannon Barber
Argh, I still can't get it to work...

Any function pointer assignment snippets?

cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

when I try...

...

oh lordy, it's somethin' to do with the class contained function...
it works with a function not in a class...

...

ahhh duh, it's gotta be static - i already knew that

...

Oh this is getting ugly... It can't be static, I gotta access stuff in the calling object... could make the properties static...

...

Is it possible to resovle this?
cannot convert from 'void (__thiscall CWorld::*)(void)' to 'void (__cdecl *)(void)'

hum, i have an idea... more to follow, in case you ever want to do this...

Edited by - Magmai Kai Holmlor on June 25, 2000 10:56:47 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
baskuenen
How about another approach:
Make your own virtual functions in a base class, and overide them in a derived class. This way you can let C++ handle the function ptrs for you. Why not put C++ to full use...

Short example:
    
class BaseWindow
{
protected:
virtual LRESULT OnPaint(WPARAM wParam, LPARAM lParam);
virtual LRESULT OnWhatever(WPARAM wParam, LPARAM lParam);
};

class AnotherWindow: public BaseWindow
{
protected:
LRESULT OnPaint(WPARAM wParam, LPARAM lParam);
};


Shannon Barber
Shannon Barber
I'll still have the same problem.

I'll have to derefence a pointer to get to the actual function I want to call.

The Render() function is in a different class...

Edited by - Magmai Kai Holmlor on June 25, 2000 11:06:09 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
baskuenen
quote:

...

CWindow* MyWindow;
CWorld* Toril;
//init
//init
//init

MyWindow->SetRenderFunc(Toril->Render);

...



Solution:
                
class CWorld
{
public:
void Render(...);
};
class CWindow
{
CWorld *pWorld;
public:
void Paint(...);
};

void CWindow:: Paint(...)
{
if (pWorld) pWorld->Render(...);
}


Please tell me it works fine ...



Edited by - baskuenen on June 25, 2000 11:14:19 PM
Shannon Barber
Shannon Barber
Yes that works
I kinda didn't want to have the window contain the world

that pWorld->Paint()
is exactally what I don't want

i want:
    
typedef void (*RenderFuncPtr)();
RenderFuncPtr MoBettaPaint;

MoBettaPaint = pWorld->Paint;

...

WM_Paint:
{
MoBettaPaint();
}break;


Edited by - Magmai Kai Holmlor on June 25, 2000 11:17:42 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
baskuenen
?

WHY?



Edited by - baskuenen on June 25, 2000 11:20:19 PM
Shannon Barber
Shannon Barber
Because

I want to perfect the form, I'll use again with other repetitive function calls

such as
lpD3D7Device->DrawPrimitive...

Edited by - Magmai Kai Holmlor on June 25, 2000 11:26:25 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
baskuenen
baskuenen
Here we go again...hope this is the click (probably not, hehe)

    
class CRendering
{
public:
virtual void Render(...);
};

class CWorld : public CRendering
{
public:
void Render(...);
};

class CWindow
{
CRendering *pRendering;
public:
void Paint(...);
};

void CWindow:: Paint(...)
{
if (pRendering) pRendering->Render(...);
}


Shannon Barber
Shannon Barber
no no no
The renderer isnt a seperate class - it's in the CWorld

but that pWorld->Render()
---------------^^

Is what I want to eliminate.

such that CWindow::RenderFunc = CWorld::RenderFunc
Which I can do - CWorld::RenderFunc just needs to be static
But it can't be static & access object specific variables (unless they too are static)

So I need a CWindow::RenderFunc = pWorld->RenderFunc
Which I can't do yet

it'll change
pWorld->Render()
to
RenderFunc();

it won't make much a difference on that one function in the msgpump, but I think it will with all the D3D->PickAFunction's


Edited by - Magmai Kai Holmlor on June 25, 2000 11:39:27 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Shannon Barber
Shannon Barber
so close...
how do you get *this to it?
hum, me thinks this be impossible.

            
class CWorld;
class CWindow;

winmain()
{
CWindow* lpWindow = new CWindow;
CWorld* lpWorld = new CWorld;

//Init...

lpWindow->SetRenderFunc(lpWorld->Render);
//...

lpWindow->Run();
}
typedef void (CWorld::* RenderFuncPtr)();

class CWindow
{
public:
SetRenderFunc(RenderFuncPtr funcptr)
{
RenderFunc = funcptr;
}
private:
RenderFuncPtr RenderFunc();
};

CWindow::MsgPump()
{
//On Idle

RenderFunc(); //Does not evaluate to a Function

}



Edited by - Magmai Kai Holmlor on June 25, 2000 12:27:25 AM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.