Sign in to follow this  

Function Pointers

This topic is 4812 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 have questions about Function Pointers a function pointers is .. int (*PtrToFunc) (int* ArrayAdress,int ArrayBoundary); and a function pointers array; int (*PtrToFunc[]) (int* ArrayAdress,int ArrayBoundary); My First Question is where can i use them? or where are you using them? second is it possible to increment these by one? (i am meaning pointer arithmetic) And i would be glad if you just show me a linklist of PointerFunction (A list that contains Functions as members).. anyway thanks..

Share this post


Link to post
Share on other sites
Everything you ever wanted to know about function pointers

I generally typedef my fp's so you can declare an array like:

typedef int (*PtrToFunc) (int* ArrayAdress,int ArrayBoundary);
PtrToFunc pointerArray[10];

Calling the fp is simple, just use it like a regular function. As for pointer arithmetic, you can do it - but be careful that you know what you're doing.

I use fp's in my message map system (technically, I use functors, which are function objects). I register a handler with the system (essentially a callback function) and that will act as a delegate for the message event when it's raised.

Share this post


Link to post
Share on other sites
Hi,

As a related example - I use pointers to member functions in my camera class. I wanted to have a camera with multiple functionalities (ground - typical FPS camera; flying - complete 3D control, primarily used for the editor; remote - used for engine cutscenes and controlled entirely by a script) - but I wanted to have the camera transition cleanly from one camera to the next. For example - at the beginning of a level, the camera might start off as remote, be told to move forward 10 units, then slerp through a given rotation (around some obstacles), and then move down and forward to its final position. It then cuts directly to a ground camera, handing control to the player.

Originally I thought this would be a case of using polymorphism - a base camera class and derived classes for each type of functionality. However, as I wanted it to move cleanly from one camera type to another (and therefore needed preservation of the camera (x,y,z) location from one camera type to another), I encapsulated all the functionality into one class, and use function pointers to change from one mode to another, as in the code below (appropriate functionality only).

Hope this gives you another idea of where to use function pointers!
Jim.

PS Any comments on this approach gratefully received!


class cCamera
{
public:

void (cCamera::*pKeyboard)();
void (cCamera::*pMouse)();

void Update() {
(this->*pKeyboard)();
(this->*pMouse)();
};

void ChangeToGroundCamera()
{pKeyboard = &cCamera::GroundCheckForKeyboardMovement;
pMouse = &cCamera::GroundCheckForMouseMovement;
};

void ChangeToFlyingCamera();
void ChangeToRemoteCamera();

void GroundCheckForKeyboardMovement();
void GroundCheckForMouseMovement();

//More functions

private:
cVector3 m_CurrentPosition;
//More data
};


Share this post


Link to post
Share on other sites

This topic is 4812 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this