|
-
Mar 15th, 2003, 09:21 AM
#1
Pointer conversion
I'm simulating the .Net delegates.
Currently it only supports non-void return types and two arguments (because that's what I currently need). It consists of two template classes, one for normal functions and one for member functions:
Code:
// Pointer to any global or static 2-arg function
template <typename RET, typename ARG1, typename ARG2>
class two_arg_fxn_ptr
{
protected:
// void * to allow for other pointer types in derived classes
void *ptr;
two_arg_fxn_ptr()
: ptr(0)
{
}
public:
typedef RET (*ptr_type)(ARG1, ARG2);
explicit two_arg_fxn_ptr(ptr_type p)
{
ptr = reinterpret_cast<void *>(p);
}
virtual RET call(ARG1 a1, ARG2 a2)
{
ptr_type p = reinterpret_cast<ptr_type>(ptr);
return (*p)(a1, a2);
}
RET operator ()(ARG1 a1, ARG2 a2)
{
return call(a1, a2);
}
};
// Pointer to any 2-arg member function.
template <typename OBJ, typename RET, typename ARG1, typename ARG2>
class two_arg_member_fxn_ptr
: public two_arg_fxn_ptr<RET, ARG1, ARG2>
{
OBJ *obj;
public:
typedef RET (OBJ::*ptr_type)(ARG1, ARG2);
two_arg_member_fxn_ptr(OBJ *o, ptr_type p)
: obj(o)
{
ptr = reinterpret_cast<void *>(p);
}
virtual RET call(ARG1 a1, ARG2 a2)
{
ptr_type p = reinterpret_cast<ptr_type>(ptr);
return (obj->*p)(a1, a2);
}
};
You see that the member function class derives from the other to allow for storage of both in a single container.
The normal version compiles fine, but the member function version fails at the two red lines with an impossible-conversion error. VC++, cryptic as always, says something about "there is no context in which this conversion would be possible".
Which is really confusing me...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|