Original Post
🔒 Locked
typedef void obj*::func*(void*) MYTYPE;
Advertisement
reminds me of something more or less terrible i came up with for my gui callbacks.
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]
#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]
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 ]
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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement