Topic: Pointers to Members (Stroustrup, C++ Prog Lang, 2nd Ed)


Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/06/28
Raw View
In article <mebust-2306951704250001@gypsy.physics.unc.edu>,
Scott Mebust <mebust@physics.unc.edu> wrote:
>In section 5.4.5 (Pointers to Members) of Bjarne Stroustrup's THE C++
>PROGRAMMING LANGUAGE, SECOND EDITION, he states that, "It is possible to
>take the address of a member of a class."
>
>Doesn't this statement apply only to member functions and not arbitrary
>class members?
>
>Could someone clarify this for me?

 The address of a non-static data member of a class
has type "pointer to member" and should be thought of as a typed
OFFSET which can be "added" to a pointer to an object of the class
to form a pointer to the corresponding member of the object.

 The same is true for non-static member functions,
which notionally are stored in objects at locations which
have offsets. This is particularly true of virtual functions
for which you can think that a virtual function is just a
function pointer -- and the pointer to virtual function
member is the offset of the function pointer in the object.
(Which is why such ptms invoke virtual behaviour).

 In practice no one actually stores functions, or
even pointers to them, in objects -- because of classes
all being the same, a single table (vtable) is created
and a single pointer to it is stored in the object.

 This is, however, just an implementation technique
enabled by optimisation theorems. Of course C++ was designed
to make these optimisations possible.



--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189





Author: bkline@cortex.nlm.nih.gov (Bob Kline Phoenix Contract)
Date: 1995/06/27
Raw View
Muzaffer Kal (muzaffer@smixedsignal.com) wrote:
: mebust@physics.unc.edu (Scott Mebust) wrote:

: >In section 5.4.5 (Pointers to Members) of Bjarne Stroustrup's THE C++
: >PROGRAMMING LANGUAGE, SECOND EDITION, he states that, "It is possible to
: >take the address of a member of a class."

: >Doesn't this statement apply only to member functions and not arbitrary
: >class members?  Perhaps I am misunderstanding the use of the word member.
: >I take it to mean both variables and functions of a class.  It is easy to
: >see how one may take the address of a member function, since a
: >(non-virtual) member function must be defined for the class, it is a piece
: >of code that will reside somewhere in memory and have an address but a
: >member variable only exists and has an address within an instance of the
: >class and thus, one could take the address of a member variable of an
: >instance of a class but not the address of a member variable of the class.

: >Could someone clarify this for me?

: >Scott Mebust

: yes you can take the address of a member and yes you are right it is
: not a pointer in the generic sense that by itself it identifies a
: block of memory. A pointer to member is usually an offset from the
: beginning of the class and it is only valid when combined with an
: object. The new operator .* is used to access pointer to members.

: hope this clarifies more than it muddies :-)

: muzo

Actually you can do both.  In addition to the pointer-to-member
you refer to you can also take the actual address of a member.
Perhaps it will ring a bell if the example has some familiar
elements to it (remember, a struct is a class with different
access defaults):

    struct foo {
 int bar;
    };

    void setbar(foo *f) {
 scanf("%d", &f->bar);
    }

Followups to comp.lang.c++, please.

--
/*----------------------------------------------------------------------*/
/* Bob Kline                                       Stream International */
/* bob_kline@stream.com               formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311                      fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/





Author: mebust@physics.unc.edu (Scott Mebust)
Date: 1995/06/23
Raw View
In section 5.4.5 (Pointers to Members) of Bjarne Stroustrup's THE C++
PROGRAMMING LANGUAGE, SECOND EDITION, he states that, "It is possible to
take the address of a member of a class."

Doesn't this statement apply only to member functions and not arbitrary
class members?  Perhaps I am misunderstanding the use of the word member.
I take it to mean both variables and functions of a class.  It is easy to
see how one may take the address of a member function, since a
(non-virtual) member function must be defined for the class, it is a piece
of code that will reside somewhere in memory and have an address but a
member variable only exists and has an address within an instance of the
class and thus, one could take the address of a member variable of an
instance of a class but not the address of a member variable of the class.

Could someone clarify this for me?

Scott Mebust





Author: kuehl@uzwil (Dietmar Kuehl)
Date: 1995/06/24
Raw View
Hi,
Scott Mebust (mebust@physics.unc.edu) wrote:
: In section 5.4.5 (Pointers to Members) of Bjarne Stroustrup's THE C++
: PROGRAMMING LANGUAGE, SECOND EDITION, he states that, "It is possible to
: take the address of a member of a class."

It is not that clear in this section but you should have realized that
taking an address of a member of a class always (if it is a function or
a data member) results in a pointer-to-member not in a normal pointer.
The reason for this special 'pointer-to-member' is rather obvious: You
have to supply the actual object when using such a pointer: either it
becomes the implicit 'this' argument to the function or it becomes the
information necessary to locate the requested data member.

Note that there is no other use of pointer-to-member than using it with
the special pointer-to-member operators (.* and ->*): neither can you
call a a member function using only a pointer-to-member (without an
object) nor can you access a member without an object.

dk
--
http://www.informatik.uni-konstanz.de/~kuehl
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic





Author: muzaffer@smixedsignal.com (Muzaffer Kal)
Date: 1995/06/24
Raw View
mebust@physics.unc.edu (Scott Mebust) wrote:

>In section 5.4.5 (Pointers to Members) of Bjarne Stroustrup's THE C++
>PROGRAMMING LANGUAGE, SECOND EDITION, he states that, "It is possible to
>take the address of a member of a class."

>Doesn't this statement apply only to member functions and not arbitrary
>class members?  Perhaps I am misunderstanding the use of the word member.
>I take it to mean both variables and functions of a class.  It is easy to
>see how one may take the address of a member function, since a
>(non-virtual) member function must be defined for the class, it is a piece
>of code that will reside somewhere in memory and have an address but a
>member variable only exists and has an address within an instance of the
>class and thus, one could take the address of a member variable of an
>instance of a class but not the address of a member variable of the class.

>Could someone clarify this for me?

>Scott Mebust

yes you can take the address of a member and yes you are right it is
not a pointer in the generic sense that by itself it identifies a
block of memory. A pointer to member is usually an offset from the
beginning of the class and it is only valid when combined with an
object. The new operator .* is used to access pointer to members.

hope this clarifies more than it muddies :-)

muzo