• Advertisement

Archived

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

Function Pointers

This topic is 6451 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

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...

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
    
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...

Share this post


Link to post
Share on other sites
They are required to load functions from a dynamically loaded library I think.

---
www.crazycrackerz.org

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);
};


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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(...);
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you''re trying to make a function pointer that points to a function inside a class, this is the syntax:

void (CWorld::*ptr)();

In this example you can point "ptr" to any function in CWorld with the appropriate prototype.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The comment beside the call in MsgPump sounds like it thinks you haven''t pointed RenderFuncPtr to anything yet.

Try changing:
"RenderFuncPtr RenderFunc();"

to this:
"RenderFuncPtr RenderFunc;"

I think the first one is a new function that returns a RenderFuncPtr, the second is a function pointer.

Share this post


Link to post
Share on other sites
I can do this! and I''m still only learning C++.
I can''t remember how to do it now (sorry, it''s at home), but I found it in the MS VC help thingy, and it''s very similar to what Baskunen suggested in his first post.

George.

"Who says computer games affect kids, imagine if PacMan affected us as kids, we'd all sit around in a darkened room munching pills and listening to repetitive music....uh oh!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by GEo

I can do this! and I''m still only learning C++.
I can''t remember how to do it now (sorry, it''s at home), but I found it in the MS VC help thingy, and it''s very similar to what Baskunen suggested in his first post.

George.

"Who says computer games affect kids, imagine if PacMan affected us as kids, we''d all sit around in a darkened room munching pills and listening to repetitive music....uh oh!"




That first post wasn''t a class contained function, nor did it go into a class.

It works with static methods (which makes perfect sense) and doesn''t with non-static methods, which also makes sense... If you have a working example of a function pointer property that points to another class''s (non-static) method - I be happy to see it! It very clurgy idea though, I''ve thought of a better way to do it... well, a different way anyhow

Share this post


Link to post
Share on other sites

  • Advertisement