Sign in to follow this  

C# equivalent of function pointers [solved]

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

OK, basically what I want to implement is a function stack for the implementation of game states. I got the idea from "Role Playing Game Programming With DirectX", but unfortunately it's in C++. It basically uses a linked list of function pointers to implement game states. It works like a stack, that is LIFO. I've tried using the System.Delegate class but I can't get any reasonable functionality out of it. Are delegates the answer? Any help is greatly appreciated, thanks! [Edited by - NickHighIQ on June 9, 2007 7:56:35 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by NickHighIQ
delegates the answer?


yes,
i haven't found any tutorials that do of very good job of explaining them but basically a delegate is a function pointer wrapped in a typesafe object

(if you put some bad data in you can get a exception instead of massive program crash)

EDIT:
i not sure but C# may have regular function pointer as part of its unsafe mode but using delegates is probably the better option

Share this post


Link to post
Share on other sites
I would prefer not to use unsafe mode if it can be helped, as you would understand. So delegates you reckon?

How are they used, specifically? I can't find any examples for exactly what I want. Do you remember VB6's CallByName function? I basically want that, only a little more sophisticated of course, so I just have a list of function names (pointers) in the stack to call to.

Does it require the delegate keyword or the System.Delegate class, or both? Or something entirely different? There is also this MethodInfo class I know nothing of as yet, but it seems to be involved in this sort of thing.

Share this post


Link to post
Share on other sites


//definition for delegate (think of it as a class definition)
//function pointer that takes no parameters
public delegate int FooFunc();
//this also works
//public delegate int FooFunc(int x, int y);

ArrayList list = new ArrayList();

void foo(){
//do stuff
}

void call(){
//call all delegates in list
foreach (FooFunc func in list){
func();
}
}


void main(){
//add foo to list
list.add(new FooFunc(foo));
//foo gets called
call();
}





Share this post


Link to post
Share on other sites
Delegates are managed and (as said before) typesafe function pointers. They can contain static (global) as well as member functions and even be passed onto unmanaged calls taking callbacks. Bottom line: they're flexible and the solution to your problem. However, don't use the System.Delegate class directly.

Here's a basic example on how to use them. In the example, I use a void( int ) function signature.


// typedef the signature
public delegate void MyDelegate( int );

// function
public void SomeFunction( int a )
{
}

public void AnotherFunction( int a )
{
}

MyDelegate myDel = new MyDelegate( SomeFunction );
// you can add new functions to an existing (multicast) delegate like so
myDel += new MyDelegate( AnotherFunction );
// and then call it
myDel( 5 );


Here's some reading material:
Delegates and Events in C# / .NET:
Working with Delegates in C#:

EDIT: Kaze's proposed solution is redundant, delegates are multicast by nature.

Share this post


Link to post
Share on other sites
Wow, thank you very much. This is exactly what I had been looking at in tutorials on the net but you made it understandable... thanks guys!

BTW, it works!! Now for the rest of the game...

Share this post


Link to post
Share on other sites

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