Re: Figure out why member function pointer doesn't work?
On 5 Apr., 03:41, Nephi Immortal wrote:
Operator->* and operator-> are very similar. Why can't I use
operator->* to call struct data's member function to pointer?
First of all, you need to recognize that the precedence of operator->
is higher than of the operator->*:
x->y->z is grouped like this: (x->y)->z
while
x->*y->z is grouped like this: x->*(y->z)
Secondly, what you are trying to do cannot work without a proxy
callable object. In order to make
(x->*y)();
work your operator->* function would need to return something that is
callable. Unfortunately, the result of ptr->*pmemfun where ptr is a
raw pointer and pmemfun is a member function pointer can only be used
as operand for the function call operator. You cannot return the
result of ptr->*pmemfun from a function and delay the function call
this way. C++ does not allow this.
If you want to support operator->* to be able to invoke member
functions you could try somehting like this:
struct bound_data_memfun {
data* ptr;
pGo pmemfun;
void operator()() const {return (ptr->*pmemfun)();}
// ^^^^^^^^^^^^^
// can only be used in combinaton with the function call
// operator.
};
struct storage {
...
bound_data_memfun operator->*(pGo pmemfun)
{
bound_data_memfun result = {pData,pmemfun};
return result;
}
...
};
IMHO it's not worth the hassle, though. None of the popular smart
pointer classes overload the operator->* and there is no harm in that.
Simply write
((*x).*y)();
and be done.
SG