pointer to class member

Discussion in 'C++' started by josh, Jan 30, 2007.

  1. josh

    josh Guest

    Hi I don't well understand if
    I have

    class Test
    {
    public:
    Test(int i){ val=i;}
    int double_val() { return val+val;}
    int val;
    };
    then

    Test t1(10), t2(100);

    // seems static variable...............
    data = &Test::val;
    func = &Test::double_val;

    // how can t1.*data AND t2.*data know the diferent address if data
    contains
    // only &Test::val ??????????
    cout << "t1 data " << t1.*data << " t2 data " << t2.*data << endl;

    the compiler print correctly the differents values ... but how is
    possible if
    I pass in data only that val address ???

    Tanks
     
    josh, Jan 30, 2007
    #1
    1. Advertising

  2. josh

    Rolf Magnus Guest

    josh wrote:

    > Hi I don't well understand if
    > I have
    >
    > class Test
    > {
    > public:
    > Test(int i){ val=i;}
    > int double_val() { return val+val;}
    > int val;
    > };
    > then
    >
    > Test t1(10), t2(100);
    >
    > // seems static variable...............
    > data = &Test::val;
    > func = &Test::double_val;
    >
    > // how can t1.*data AND t2.*data know the diferent address if data
    > contains
    > // only &Test::val ??????????


    Because you're explicitly specifying it.

    > cout << "t1 data " << t1.*data << " t2 data " << t2.*data << endl;
    >
    > the compiler print correctly the differents values ... but how is
    > possible if I pass in data only that val address ???


    When accessing it, you're not writing just *data, but t1.*data. So it's easy
    for the compiler to know that you want t1.val and not t2.val.
     
    Rolf Magnus, Jan 30, 2007
    #2
    1. Advertising

  3. josh

    josh Guest

    Ok but when should I use this methodology?

    and in a book is wrote:
    >>> a pointer to a member

    provides only an offset into an object of the member's class at which
    that member can
    be found.

    so which is the difference between an offset and a pointer???????
     
    josh, Jan 30, 2007
    #3
  4. On Jan 30, 3:42 pm, "josh" <> wrote:
    > Ok but when should I use this methodology?


    Rarely as far as I'm concerned.I'm not saying that there are no
    situations where this is not useful, but for most occasions there are
    other ways to solve the problem.

    > and in a book is wrote:
    >
    > >>> a pointer to a member

    >
    > provides only an offset into an object of the member's class at which
    > that member can be found.
    >
    > so which is the difference between an offset and a pointer???????


    A pointer is the absolute address to something, the number of bytes
    from the beginning of the address-space to the thing. An offset is
    relative, the number of bytes from the beginning of something to some
    other thing. A pointer to a member is an offset from the start of the
    instance of a class to the member.

    So to get the address of the member you could take the address of the
    class and add the offset in the member-pointer.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 30, 2007
    #4
  5. josh wrote:
    > Ok but when should I use this methodology?
    >
    > and in a book is wrote:
    >>>> a pointer to a member

    > provides only an offset into an object of the member's class at which
    > that member can
    > be found.
    >
    > so which is the difference between an offset and a pointer???????


    Whatever the author meant by "offset" is not actually just an offset
    (which is usually a relatively small integral value). Pointers to
    members are more complicated, calling them "only an offset" is trying
    to make it simpler to understand that in order to use it you still
    need an instance of the class.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 30, 2007
    #5
  6. josh

    Rolf Magnus Guest

    josh wrote:

    > Ok but when should I use this methodology?


    Well, I have never needed to use a pointer to data member, and actually
    don't know what I would need it for, but pointers to member functions on
    the other hand can be useful e.g. for callback functions.
     
    Rolf Magnus, Jan 30, 2007
    #6
  7. josh

    peter koch Guest

    On Jan 30, 4:53 pm, Rolf Magnus <> wrote:
    > josh wrote:
    > > Ok but when should I use this methodology?

    > Well, I have never needed to use a pointer to data member, and actually
    > don't know what I would need it for, but pointers to member functions on
    > the other hand can be useful e.g. for callback functions.


    Pointers to datamembers can be useful when creating intrusive
    structures. I have used that technique to create e.g. intrusive lists
    (which IMHO are useful far more often than std::list). But I admit
    they are rare beasts.

    /Peter
     
    peter koch, Jan 30, 2007
    #7
  8. josh

    josh Guest

    On 30 Gen, 16:53, Rolf Magnus <> wrote:
    > josh wrote:
    > > Ok but when should I use this methodology?

    >
    > Well, I have never needed to use a pointer to data member, and actually
    > don't know what I would need it for, but pointers to member functions on
    > the other hand can be useful e.g. for callback functions.


    can you make, please, a concrete example?
     
    josh, Jan 31, 2007
    #8
  9. josh

    Rolf Magnus Guest

    josh wrote:

    > On 30 Gen, 16:53, Rolf Magnus <> wrote:
    >> josh wrote:
    >> > Ok but when should I use this methodology?

    >>
    >> Well, I have never needed to use a pointer to data member, and actually
    >> don't know what I would need it for, but pointers to member functions on
    >> the other hand can be useful e.g. for callback functions.

    >
    > can you make, please, a concrete example?


    You can use them e.g. with the standard algorithms, something like:

    std::vector<GraphicObject*> vec;
    std::for_each(vec.begin(), vec.end(), std::mem_fun(&GraphicObject::draw));
     
    Rolf Magnus, Jan 31, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  2. Fraser Ross
    Replies:
    4
    Views:
    1,067
    Fraser Ross
    Aug 14, 2004
  3. Stephen Howe
    Replies:
    2
    Views:
    298
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    293
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    170
    somenath
    Aug 29, 2013
Loading...

Share This Page