A difference? Purpose of ->*?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Del Buono

    A difference? Purpose of ->*?

    AFAIK...

    Order of precedence of operators:

    post ++
    post --
    ..
    ..
    ..
    ->
    ..
    pre ++
    ..
    ..
    ..
    * (indirection)
    sizeof
    new
    delete
    (type) (typecast)
    ..*
    ->*
    ..
    ..
    |=
    ,

    So what exactly is the point of .* and ->* (other than giving maybe a neater
    or other way of writing things)? Aren't these the same? (Assume class A is
    defined with a pointer to an integer "bar"):

    A foo;
    A* pFoo = new A;
    foo.bar = 4;
    *foo.bar = 4;
    pFoo->*bar = 4;
    *pFoo->bar = 4;

    As far as I can tell, all of these assignments perform the same task. Now,
    I'm sure it is much easier to read pFoo->*bar than *pFoo->bar (it appears in
    the latter example that pFoo is trying to be dereferenced). Is that the only
    reason for the operator?

    -- Matt
    ..


  • Matthew Del Buono

    #2
    Re: A difference? Purpose of ->*?


    "Matthew Del Buono" <MattDelB@nospa m.com> wrote in message
    news:2Eepc.2500 6$Lm3.5077@lake read04...[color=blue]
    > AFAIK...
    >
    > Order of precedence of operators:
    >
    > post ++
    > post --
    > .
    > .
    > .
    > ->
    > .
    > pre ++
    > .
    > .
    > .
    > * (indirection)
    > sizeof
    > new
    > delete
    > (type) (typecast)
    > .*
    > ->*
    > .
    > .
    > |=
    > ,
    >
    > So what exactly is the point of .* and ->* (other than giving maybe a[/color]
    neater[color=blue]
    > or other way of writing things)? Aren't these the same? (Assume class A is
    > defined with a pointer to an integer "bar"):
    >
    > A foo;
    > A* pFoo = new A;
    > foo.bar = 4;
    > *foo.bar = 4;
    > pFoo->*bar = 4;
    > *pFoo->bar = 4;
    >
    > As far as I can tell, all of these assignments perform the same task. Now,
    > I'm sure it is much easier to read pFoo->*bar than *pFoo->bar (it appears[/color]
    in[color=blue]
    > the latter example that pFoo is trying to be dereferenced). Is that the[/color]
    only[color=blue]
    > reason for the operator?
    >
    > -- Matt
    > .
    >
    >[/color]

    Actually as I'm writing this program, I have found one instance in which the
    *foo->bar method would be required:

    class foo
    {
    public:
    int* bar();
    }
    ....
    *foo->bar() = 4; // legal -- set to 4 at address returned by bar()
    foo->*bar() = 4; // not legal -- cannot dereference a function
    // (not an address)

    Am I right? (I didn't try to compile this example)

    -- Matt




    Comment

    • Jacques Labuschagne

      #3
      Re: A difference? Purpose of -&gt;*?

      Matthew Del Buono wrote:[color=blue]
      > So what exactly is the point of .* and ->* (other than giving maybe a neater
      > or other way of writing things)? Aren't these the same? (Assume class A is
      > defined with a pointer to an integer "bar"):
      >[/color]

      What if you're trying to apply a pointer-to-member function to a pointer
      to an object?

      void my_class::apply _to_each_elemen t(void (member_type::* member_func)())
      {
      vector<member_t ype*>::iterator i = members_.begin( );
      for (; i != members_.end(); ++i){
      ((*i)->*member_func)( );
      }
      }

      Comment

      • red floyd

        #4
        Re: A difference? Purpose of -&gt;*?

        Matthew Del Buono wrote:
        [color=blue]
        > [redacted][/color]

        You misunderstand how pointer-to-member works. I suggest you go back
        to your C++ text and read up on it.

        #include <iostream>
        using namespace std;

        class A {
        public:
        int foo;
        int bar;
        };

        int main(int, char *[])
        {
        int A::*member;

        A anA;
        anA.foo = 2;
        anA.bar = 3;
        member = &A::foo;
        cout << anA.*member << endl; // prints 2;
        member = &A::bar;
        anA.*member = 7;
        cout << anA.bar << endl; // prints 7;
        return 0;
        }

        Comment

        • John Harrison

          #5
          Re: A difference? Purpose of -&gt;*?

          [color=blue]
          >
          > Am I right? (I didn't try to compile this example)
          >[/color]

          If you had tried you would have found that you have completely the wrong
          idea.

          Read up on pointers to member functions, and pointers to members (this last
          one is really obscure, many books won't cover it at all, and I have never
          seen a meaningful program that uses them).

          john


          Comment

          Working...