Skip to main content
gamedev.net
🔒 Locked

What does this code mean?

Started by KWest Aug 27, 2006 at 10:37 AM 7 replies 2.1k views
Original Post
KWest
KWest
I Found this code on the internet but having trouble understanding it: typedef void (* mFunctionPointer)( void * lpUserData ); I Think it declares a pointer to a function type. But can someone give me some examples of situations where a variable of this type might be used. Are there any other ways of achieving the same results, if so what are the pros and cons of those alternatives?
Driv3MeFar
Driv3MeFar
Quote:
Original post by KWest
But can someone give me some examples of situations where a variable of this type might be used.


Function pointers are used for callback functions. For example, in my game engine, I use callbacks to communicate between the engine and the actual game code. Whenever the engine recieves a network message, or a physics event such as a collision occurs, the engine calls a callback function to the game code so that the game can deal with the collision/net message however it sees fit. By supplying my engine a function pointer, my engine never needs to know about any of the actual game code, and can stay nice and modular.
izhbq412
izhbq412
Quote:
Are there any other ways of achieving the same results, if so what are the pros and cons of those alternatives?
You could use a function object.
___The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
TheBlackJester
TheBlackJester
Quote:
Original post by Anonymous Poster
defines mFunctionpointer as a pointer to a function that returns void and takes a void * as argument.


This is close, but not quite. It's actually declaring mFunctionpointer as a function pointer TYPE that you can use to instantiate function pointers that return void and take a void* instance:

// Make an instance of a function pointermFunctionpointer myFunctionPtr; // A void function..void Foo(void*){    // do something}// Assign the void function to the function pointermyFunctionPtr = Foo;// Now you could call the function like this..void* pBar;myFunctionPtr(pBar);

"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I'm reaching up and reaching out. I'm reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one's been. We'll ride the spiral to the end and may just go where no one's been." - Maynard James Keenan <
janta
janta
A pure virtual class/Interface class is another nice way of achieving the same result, though one could argue in certain situation where performance is critical, the cost of calling a virtual function would be a sufficient reason for not doing that.

Examples:
typedef void (*PrintCallback)(const char*);class MyClass{public:    void PrintName() { pPrinterCallback(szName) };private:    printCallback pPrinterCallback;    char* szName;}


Without funtion pointer one could use

class Printer{public:    virtual void Print(const char* _szString) = 0;}class MyClass{public:    void PrintName() { pPrinter->Print(szName) };private:    Printer* pPrinter;    char* szName;}


Of course the second method would require you to define a class that inheritates form Printer and which actually defines what its Print method does.
In my opinion, the nice point with that method is that when it comes to do more elaborated stuff, you are not limited to just one function, you can build a full class and organize everything in a nice object oriented way.

For instance, that would be possible:

class MyClass: public Printer{public:    MyClass() { pPrinter = this; }    void PrintName() { pPrinter->Print(szName) };private:    void Print(const char* _szString) { std::cout << szName << std::endl; }    Printer* pPrinter;    char* szName;}


The java's implementation of listeners inspired me that.
It might not be perfect though, I'd like to know what do other people think.
ajas95
ajas95
Quote:
Original post by janta
A pure virtual class/Interface class is another nice way of achieving the same result,


It's not quite the same. The virtual call is not reassignable at run-time. Of course, the same function pointer can't target both member functions and statics, which is why I like to use Fast Delegates for all my virtual indirection.

janta
janta
What do you mean ?

You can have:

class AnImplementationOfPrinter: public Printer { ... }
class AnotherImplementationOfPrinter : public Printer { ... }

then set the "pPrinter" to any instance of those classes at run time.

Didi I miss your point ?
nife
nife
Listener classes could also be used instead of functors.
Killers don't end up in jailThey end up on a high-score!

Topic Locked

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

Sign in to reply to this topic.