Casting

I am writing a delegate class for educational purposes and have run into a small problem. The delegate should be able to call not only functions, but also member methods, which means that I need to save a pointer to a method:

void (classname::*methodPtr)(...);

And I need to store pointers to methods from different classes and with different lists of arguments. At first I just wanted to cast a pointer to the void * method, but the compiler dies with an invalid cast error. It turns out that sizeof (methodPtr) == 8 (the 32-bit system is here), but it also fails on unsigned long long (the same compiler error - invalid cast). How to save a universal method pointer?

I know this is not safe. I have other security mechanisms, just focus on my question.

+3
source share
2 answers

No. You use runtime inheritance if you need an abstraction, and create a derived class that is customized to your needs, or preferably just create a simple old functor with a function. Check boost::bindboth boost::function(as in the standard for C ++ 0x) and how this should be done - if you can still read them in all macroresistances.

+4
source

DeadMG. , - , -. , (, ..), - " ", . , , "" , -.

+1

All Articles