redirection casting?

Discussion in 'C++' started by dave, Oct 9, 2005.

  1. dave

    dave Guest

    Why is this the same:
    (*(*pCurrent).pData).name

    as this:
    pCurrent->pData->name

    what is the benefit to the first? if any?
    why are the parenthesis important?

    thanks
     
    dave, Oct 9, 2005
    #1
    1. Advertising

  2. dave

    Marcus Kwok Guest

    dave <> wrote:
    > Why is this the same:
    > (*(*pCurrent).pData).name
    >
    > as this:
    > pCurrent->pData->name


    The x->y notation is really shorthand for (*x).y; it was originally used
    in C to access members of a struct through a pointer, since passing
    structs by value can be expensive. Therefore,

    (*pCurrent).pData == pCurrent->pData

    Applying this again to pData, we have
    (*(*pCurrent).pData).name == (pCurrent->pData)->name
    == pCurrent->pData->name


    > what is the benefit to the first? if any?
    > why are the parenthesis important?


    The parentheses are important because without them, it would be
    interpreted as *(pCurrent.pData) which is an error, since pCurrent is a
    pointer and pointers cannot have fields; however, (*pCurrent) is the
    struct (or class) pointed to by pCurrent, so that is allowed to have a
    field named pData.

    --
    Marcus Kwok
     
    Marcus Kwok, Oct 9, 2005
    #2
    1. Advertising

  3. dave

    Greg Guest

    dave wrote:
    > Why is this the same:
    > (*(*pCurrent).pData).name
    >
    > as this:
    > pCurrent->pData->name
    >
    > what is the benefit to the first? if any?
    > why are the parenthesis important?
    >
    > thanks


    The "arrow" operator provides access to the members and methods of a
    class or struct, given a pointer to the struct or class instance. The
    "." operator provides the same access, given a direct instance of a
    struct or class object. Lastly, the dereference operator, *, obtains a
    the instance of a struct or class object given a pointer to it.

    When these three operators are considered together, it becomes clear
    that dereferencing and then applying the period operator to a pointer
    is equivalent to applying the arrow operator to that pointer. In other
    words:

    pCurrent->pData->name

    should be equivalent to this:

    **pCurrent.pData.name

    But it is not. The catch is that the order in which the * and the .
    operators are applied to this expression affects the outcome. In fact
    the compiler follows a strict "order of precedence" when deciding
    whether to apply the * or the . operator first. When evaluating the
    above expression the compiler appies the operators in this order:

    **pCurrent.pData.name
    43 1 2 // <- order of operator evaluation

    The problem is that trying to apply the operators in this order, fails.
    Instead, the compiler has to alternate between the two . and *
    operators in order for this expression to make sense. Since parentheses
    have a very high precedence, using them forces the compiler to apply
    the operators in the desired order: first the *, then a ., then the
    other *, and finally the last . before "name" like so

    (*(*pCurrent).pData).name
    3 1 2 4

    Whether to use the -> or *. operator is largely a matter of preference
    in this case. Neither is more efficient or better than the other it
    terms of the compiled code.

    Greg
     
    Greg, Oct 9, 2005
    #3
    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. MadHatter

    input redirection

    MadHatter, Oct 14, 2003, in forum: Perl
    Replies:
    6
    Views:
    695
    MadHatter
    Oct 16, 2003
  2. S
    Replies:
    1
    Views:
    817
  3. kevin
    Replies:
    11
    Views:
    5,810
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    529
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    565
    James Kanze
    Dec 20, 2009
Loading...

Share This Page