Re: Figure out why member function pointer doesn't work?
On 4/1/2011 9:15 PM, Nephi Immortal wrote:
Please look at my code below. You may notice operator->* with
comments. Figure out why it does not make any sense.
struct data;
typedef void ( data::*pGo )();
struct data {
char x;
char y;
pGo Go;
void Run() {
}
};
struct storage {
data *pData;
data *operator->() {
return pData;
}
storage&operator->*( pGo p ) {
( pData->*p )();
return *this;
}
};
int main()
{
data d; storage s;
s.pData =&d;
s->x = 1; s->y = 2; s->Go =&data::Run;
( d.* d.Go )(); // OK
( d.* s->Go )(); // OK
s.operator->*( s->Go ); // ??? Works fine
s->*( s->Go ); // ??? Works fine
( s->* s->Go )(); // Should be, but does not work!
What do you mean by "does not work"? Does it compile? If not, what's
the error message, what in it is unclear? If it does compile, do you
get an error when running?
Have you checked operator precedence? Can you place the parentheses in
the last expression to indicate how you think it should be evaluated?
Does it "work" after that? What's different (if anything) when you add
parentheses?
return 0;
}
V
--
I do not respond to top-posted replies, please don't ask