Skip to main content
gamedev.net
🔒 Locked

storing member function pointer a ints

Started by mrrolf810p 3 replies 600+ views
Original Post
mrrolf810p
mrrolf810p
I'm trying to store a member function pointer into a generic data type, uint for example. I've read that member function pointers can take up to 16 bytes using the MSVC compiler, so I'm trying to store the MFP into an array of ints.

template< typename _METHOD >
foo( _METHOD method )
{
	unsigned int methodStorage[4];
	reinterpret_cast<_METHOD>(methodStorage) = method;
}

This isn't compiling, it's giving me the C2440 error. Is there anyway to do this? Thanks
dave
dave
According to MSDN you cannot convert this to a UDP, so instead try casting to an unsigned int.

Dave
hplus0603
hplus0603
The problem is that methodStorage is a pointer type. You want something like:

  *(int (T::*)(char *, void *))methodStorage = theMethod;


Which, in your case, would probably be written something like:

template< typename _METHOD >foo( _METHOD method ){	unsigned int methodStorage[4];	*reinterpret_cast<_METHOD*>(methodStorage) = method;}


However, it's un-clear what you're trying to do, and whether it's actually possible to make work using only C++. I recall a web page that describes how to implement delegates in MSVC, using a little bit of inline assembly to get around compiler type casting limitations -- you might want to google for that.
enum Bool { True, False, FileNotFound };
mrrolf810p
mrrolf810p
Quote:
Original post by hplus0603
	*reinterpret_cast<_METHOD*>(methodStorage) = method;



This works for me. Thanks!

I'm trying my hand at making my own functor/delegate class. Storing the member function pointers in a generic data type cleans things up when I use the class.
ajas95
ajas95
You should definitely read this article right now.

Topic Locked

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

Sign in to reply to this topic.