Topic: Question about pointer to member function


Author: Rob Stewart <stew@datalytics.com>
Date: 1996/05/21
Raw View
Ian T Zimmerman wrote:
>
> In article
> <Pine.SUN.3.91-heb-2.05.960515192911.6566A-100000@bamba.cs.huji.
ac.il>
> Moshe Ben-Ezra <moshe@cs.huji.ac.il> writes:
>
> >  In Bjarne Stroustrup book: 'The C++ programming language 2nd
> > edition' section 5.2.3 page 147 "Self-Reference" it is stated that
> > the primary use of 'this' is when manipulating pointers directly -
> > like in implementing double link list. (and that the use of 'this'
> > is unnecessary when referring to members).
>
> >
> > following 5.2.3 one can expect to use the syntax:
> >
> >       (*pf)();

Actually, C doesn't (nor does C++, for that matter) require you
to dereference the function pointer.  So, this is equivalent to
your expression:

 pf();

> No.  Understand that "->*" and ".*" and "::*" and "::&" are single
> tokens of C++ syntax.  They are not `composed of' "->" and "*" etc.,
> at least as far as the parser is concerned.
>
> > However this syntax produces an error *Using g++* and only the
> > following syntax is accepted:
>
> > > >   (this->*pf)();
> >
> >
> > The question is therefore: Does this agree with C++ standard ?
>
> Yes.
>
> > and if it does why is the this needed ? - it's not consistant with
> > 5.2.3.
>
> It is.  Pointers to members are not members.

The crux of the matter is that pf is a pointer to a class
member.  For it to operate on an object, you must give it an
object on which to operate.  Within a mf, you could expect it to
include the implicit "this->" available to all other dm and mf
accesses.

However, as Ian pointed out, the use a pointer to member
function requires the use of the "->*" token.  Since this has
similar behavior to "->," it requires a pointer on the left
side.  Obviously, you need to supply "this" as your pointer to
operate on the current object.  So now we have "this->*pf," but
it still won't compile.  The reason is that function notation,
"()," has higher precedence than "->*," so we must group
"this->*pf" to prevent the standard grouping, "pf()."

> --
> Ian T Zimmerman           +-------------------------------------------+
> Box 13445                 I    With so many executioners available,   I
> Berkeley CA 94712 USA     I suicide is a really foolish thing to do.  I
> mailto:itz@rahul.net      +-------------------------------------------+
> ---
> [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
>                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
>   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
>   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
>   Comments? mailto:std-c++-request@ncar.ucar.edu
> ]

--
Robert Stewart  | My opinions are usually my own.
Datalytics, Inc. | stew@datalytics.com


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Moshe Ben-Ezra <moshe@cs.huji.ac.il>
Date: 1996/05/15
Raw View
In Bjarne Stroustrup book: 'The C++ programming language
2nd edition' section 5.2.3 page 147 "Self-Reference" it is stated
that the primary use of 'this' is when manipulating pointers directly
- like in implementing double link list. (and that the use of 'this'
is unnecessary when referring to members).

in section 5.4.5 "pointers to members" there is an example of
using pointer to member function. The reference is made from main().
However the question of referring a member from withing the class
is not addresses.

following 5.2.3 one can expect to use the syntax:

 (*pf)();

where pf is a pointer to a member function. - This would also
agree with standard C.

However this syntax produces an error *Using g++*
and only the following syntax is accepted:

> >  (this->*pf)();


The question is therefore: Does this agree with C++ standard ?
and if it does why is the this needed ? - it's not consistant
with 5.2.3.


thanks moshe.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: itz@rahul.net (Ian T Zimmerman)
Date: 1996/05/16
Raw View
In article
<Pine.SUN.3.91-heb-2.05.960515192911.6566A-100000@bamba.cs.huji.ac.il>
Moshe Ben-Ezra <moshe@cs.huji.ac.il> writes:

>  In Bjarne Stroustrup book: 'The C++ programming language 2nd
> edition' section 5.2.3 page 147 "Self-Reference" it is stated that
> the primary use of 'this' is when manipulating pointers directly -
> like in implementing double link list. (and that the use of 'this'
> is unnecessary when referring to members).

>
> following 5.2.3 one can expect to use the syntax:
>
>  (*pf)();
>

No.  Understand that "->*" and ".*" and "::*" and "::&" are single
tokens of C++ syntax.  They are not `composed of' "->" and "*" etc.,
at least as far as the parser is concerned.

> However this syntax produces an error *Using g++* and only the
> following syntax is accepted:

> > >  (this->*pf)();
>
>
> The question is therefore: Does this agree with C++ standard ?

Yes.

> and if it does why is the this needed ? - it's not consistant with
> 5.2.3.

It is.  Pointers to members are not members.
--
Ian T Zimmerman           +-------------------------------------------+
Box 13445                 I    With so many executioners available,   I
Berkeley CA 94712 USA     I suicide is a really foolish thing to do.  I
mailto:itz@rahul.net      +-------------------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]