Skip to main content
gamedev.net
🔒 Locked

typedef void obj*::func*(void*) MYTYPE;

Started by pcxmac Jul 14, 2003 at 6:36 PM 5 replies 1.1k views
Original Post
pcxmac
pcxmac
typedef void obj*::func*(void*) MYTYPE; where obj is any arbitrary class and func is a func pointer conforming to void f (void*); how do i make a type that will enclose both func and obj*? i wanna keep it as simple as possible.
sig
Ishan
Ishan
how about this?


template <class T>
struct functor{
void (T::*ptr)(void);
};

and so on...make methods for executing, and a constructor, and so on
Trienco
Trienco
reminds me of something more or less terrible i came up with for my gui callbacks.


#define METHOD(x) (void(UIBaseObject::*)(void*))&x

class Callback {
private:
UIBaseObject* obj;
void(UIBaseObject::*func)(void*);
public:
Callback() : obj(0), func(0) {}
void Set(UIBaseObject* o, void(UIBaseObject::*f)(void*)) {obj=o; func=f;}
operator()(void* p) {if (obj && func) (obj->*func)(p);}
};

example for usage in a form or whatever:

Callback OnMouseDown;
OnMouseDown.Set(this, METHOD(Form::MouseDownFunc));
OnMouseDown(ptr);


doint this as template and replacing UIBaseObject with T should probably work. im still trying to find an elegant solution for different arguments (in number and type) unfortunately the ellipse isnt meant for templates and so far every solution ended with horrible ways you would have to write your callbacks by accessing all arguments via the arg-list.

if you dont like the () operator just make it a function, but i think it feels more natural to just call the class like it was a function. depending on how you use it you might do without the typecast macro.

[edited by - Trienco on July 15, 2003 8:04:23 AM]
f@dzhttp://festini.device-zero.de
foofightr
foofightr
Trienco... my eyes!! The goggles... dey do nothing!

You should put a warning label on code like that!
Fruny
Fruny

class Class
{
public:
void MemberFunction(void*);
};


// typedef for a pointer to member function

typedef void (Class::*mfptr_type)(void*);
mfptr_type ptr = &Class::MemberFunction;

int main()
{
Class obj; // Create an object

(obj.*ptr)(), // Call the function on the object


Class* pobj = new Class; // Create an object

(pobj->*ptr)(); // Call the function on the object



Important note : you cannot use a pointer to (non-static) member function where an ''ordinary'' pointer to function is expected. Don''t even try. You cannot do anything with bound pointers (obj.*ptr or pobj->*ptr) either, only call them. Storing them is impossible.

See the "Function Pointers" link in my signature.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Trienco
Trienco
quote:
Original post by foofightr
Trienco... my eyes!! The goggles... dey do nothing!

You should put a warning label on code like that!


and give you a chance to avoid the headache? no way.

maybe a small one like "do not use unless youre REALLY desperate" ,-)

in fact, the uibaseobject is pretty pointless, as its casted anyway and polymorphism doesnt apply.
but it in the end it seemed the easiest way to have the callbacks in the base clase, the used functions in the form derived from dialogue and a button calling this function on click.

oh damn.. i followed frunys link and the part about functors is pretty much the same. just explained a lot more and better *g*

[edited by - Trienco on July 15, 2003 8:20:41 AM]
f@dzhttp://festini.device-zero.de
daerid
daerid
Boost.Function

End of thread
daerid@gmail.com

Topic Locked

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

Sign in to reply to this topic.