Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Figure out why member function pointer doesn't work?

Reply
Thread Tools

Figure out why member function pointer doesn't work?

 
 
Nephi Immortal
Guest
Posts: n/a
 
      04-02-2011
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!

return 0;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-02-2011
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
 
Reply With Quote
 
 
 
 
Qi
Guest
Posts: n/a
 
      04-02-2011
On 2011-4-2 9:15, Nephi Immortal wrote:
> ( s->* s->Go )();


"( s->* s->Go )(); "
==
(( s->* s)->Go )();

So it won't compile.


--
WQ
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      04-02-2011
On Apr 1, 9:55*pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> 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?


Victor,

You asked the same questions. I searched using google operator->*
across websites. There are a lot of operator->, but nothing showed
operator->*. There is no book mentioned it either.
I can say operator->* is rarely used. Please do a favor for me. Do
your homework to do research? Okay? If you succeed, the information
should be added to the book. You will be author as C++ book writer.


>
>
>
> > * *return 0;
> > }

>
> V
> --
> I do not respond to top-posted replies, please don't ask- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-03-2011
On Apr 2, 2:15 am, Nephi Immortal <immortalne...@gmail.com> 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 ) {


Here you define an operator->* which takes a storage and a
data::*pGo, and returns a storage. Why? What are you trying to
achieve.

> ( 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!


s->*s->Go returns a storage&. You then try to call it as a
function. Since storage has no overloaded operator(), it fails.
What do you expect?

> return 0;
> }


--
James Kanze
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      04-05-2011
On Apr 3, 11:54*am, James Kanze <james.ka...@gmail.com> wrote:
> On Apr 2, 2:15 am, Nephi Immortal <immortalne...@gmail.com> 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 ) {

>
> Here you define an operator->* which takes a storage and a
> data::*pGo, and returns a storage. *Why? *What are you trying to
> achieve.
>
> > * * * * * * * * ( 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!

>
> s->*s->Go returns a storage&. *You then try to call it as a
> function. *Since storage has no overloaded operator(), it fails.
> What do you expect?
>
> > * * * * return 0;
> > }


James,

Do you know how to implement operator->* like I wrote operator->?
Operator->* and operator-> are very similar. Why can’t I use operator-
>* to call struct data’s member function to pointer?

 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      04-05-2011
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
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      04-05-2011
On 5 Apr., 09:22, SG wrote:
> 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)


the relevant cases here are actually

x->y() is equiv to (x->y)()
x->*y() is equiv to x->*(y()) AND NOT to (x->*y)()

Operators -> and () form postfix expression while ->* and .* are
pointer-to-member operators with a lower precedence. Also:

" 5.5 Pointer-to-member operators [expr.mptr.oper]
[...]
6 If the result of .* or ->* is a function, then that result can
only be used as operand for the function call operator (). "

SG
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can't figure out syntax error with templates/member function pointers Aaron Walker C++ 4 10-01-2005 04:19 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57