I have my code organized as following
class MyClass
{
public:
typedef void (MyClass::Ptr2func)();
}
struct abc
{
MyClass::Ptr2func ptr;
bool result;
}
void main()
{
MyClass myCls;
abc var;
//here is a lot of decision making code and pointer initializations
//I want to copy the pointer to function in another variable
MyClass::Ptr2func tempPtr;
tempPtr=var.ptr;
}
When I try to copy the var.ptr into tempPtr it gives me a compilation error that the argument list is missing. Also it gives me compilation error on myCls.*(var.ptr()); Is there a precedence issue? I have tried using parenthesis but nothing works. I hope someone can help me out on this.
Thanks and regards,
Saba Taseer
I believe that the problem is that your typedef
Is not defining a typedef for a pointer to member function, but for a member function type. The typedef for a member function pointer would be
Notice the explicit pointer involved here. The type
is actually a typedef for the type of a member function inside of
MyClassthat takes no arguments and returns void.As for your final question, the proper syntax for calling a pointer to a member function is (I believe)
Notice that you must parenthesize the expression
(myClass.*var.ptr)before trying to call it as a function, since the codeMeans “dereference the pointer-to-member returned by
var.ptr()relative to objectmyCls.Hope this helps!