Jump to content
  • Advertisement
Sign in to follow this  
mrrolf810p

storing member function pointer a ints

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

Share this post


Link to post
Share on other sites
Advertisement
According to MSDN you cannot convert this to a UDP, so instead try casting to an unsigned int.

Dave

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!