Skip to main content
gamedev.net
🔒 Locked

POinter to member function

Started by osh Nov 8, 2004 at 9:53 AM 7 replies 850+ views
Original Post
osh
osh
Hello all! Is there any way to cast pointer to member function to general pointer to function? I have one class that has few pointers to func and I need to pass to it funcs from another class(and I cannot use pointer to member func 'cos that later class is template). I could use functors, but I'm trying to avoid using virtual functions... Thx
ajas95
ajas95
Good ol' reliable:

http://www.codeproject.com/cpp/FastDelegate.asp

Learn it, use it, love it.
SiCrane
SiCrane
But in a word, no. Normal function pointers are generally always 4 bytes in size. Member function pointers can get much bigger. For example, on MSVC they can be up to 16 bytes in size, which means that the member function pointer contains information that a normal function pointer doesn't have to deal with.
osh
osh
Ok, one more thing. Is it possible to cast pointer to member function of one class to ptr to member func of its base class?

Edit: I've briefly looked at suggested article at codeguru and found answer to my question - no. I'll try to redesign my code to get it workin'. Thx anyway

[Edited by - osh on November 8, 2004 11:47:19 AM]
SiCrane
SiCrane
Quote:
Original post by osh
Ok, one more thing. Is it possible to cast pointer to member function of one class to ptr to member func of its base class?

No, but it is possible to cast a pointer to member function of a base class to a pointer to member function of a derived class.
ajas95
ajas95
osh--
Yeah, the answer is no. But I posted the link because the author, because he was in the same situation you are, built a type (FastDelegate<>) that acts as a function pointer that takes both memeber functions and non-member functions (as long as they're compatible in arguments and return type), and evaluates at compile time to the optimal asm code for making the function call. And it works on all platforms/compilers.

Of course, to do this requires some really insane coding... all under-the-covers, of course, so you don't have to worry about it, but here's a snippet that might drop your jaw:
template <class OutputClass, class InputClass>inline OutputClass horrible_cast(const InputClass input){	union {		OutputClass out;		InputClass in;	} u;	// Cause a compile-time error if in, out and u are not the same size.	// If the compile fails here, it means the compiler has peculiar	// unions which would prevent the cast from working.	typedef int ERROR_CantUsehorrible_cast[sizeof(InputClass)==sizeof(u) 		&& sizeof(InputClass)==sizeof(OutputClass) ? 1 : -1];	u.in = input;	return u.out;}
TheBluMage
TheBluMage
It is possible to convert a member function pointer to a global function pointer. Under the hood, member functions are regular functions that pass a this pointer to the function automatically. Depending on what calling conventions and possibly what platform you're using the following conversion may actually work properly:
//Pointer to a MyObject member function that takes no parameterstypedef void (MyObject::*MemberFunction)(void);//Pointer to a global function that takes a MyObject* as a parametertypedef void (*GlobalFunction)(MyObject*);...MemberFunction mFunc = &MyObject::MyFunction;GlobalFunction gFunc = *((GlobalFunction*) &mFunc);MyObject o;//Now this:(o.*mFunc)();//Does the same as this:gFunc(&o);

Again, this can be very dependant on what calling conventions you use and how your compiler treats function pointers. In your case you probably need to be able to call the function without passing a pointer to the object it should be called on. In that case, do as was mentioned in the above post and create a wrapper function that will call it for you and has the same signature/return type.
snk_kid
snk_kid
Quote:
Original post by SiCrane
Quote:
Original post by osh
Ok, one more thing. Is it possible to cast pointer to member function of one class to ptr to member func of its base class?

No, but it is possible to cast a pointer to member function of a base class to a pointer to member function of a derived class.


You probably wouldn't even need to do such a thing, you probably wont an in-direct polymorphic call instead e.g.:

#include <iostream>struct foo {   virtual void do_bar() const { std::cout << "foo\n"; }   virtual ~foo() {}};struct bar : foo {   void do_bar() const {      foo::do_bar();      std::cout << "bar\n\n";   }};int main() {   typedef void (foo::*foo_mem_func)() const;   typedef void (bar::*bar_mem_func)() const;   foo_mem_func f_func(&foo::do_bar);   bar_mem_func b_func(&bar::do_bar);   bar b;   const foo& f_ref(b);   b.do_bar(); //direct non polymorphic call.   f_ref.do_bar(); //direct polymorphic call.   (b.*b_func)();   //in-direct non polymorphic call.   (b.*f_func)();   //in-direct non polymorphic call (if i remember correctly).   (f_ref.*f_func)(); //in-direct polymorphic call.   }

Topic Locked

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

Sign in to reply to this topic.