6.12 — Member selection with pointers and references

It is common to have either a pointer or a reference to a struct (or class). As you learned previously, you can select the member of a struct using the member selection operator (.):

This syntax also works for references:

However, with a pointer, you need to dereference the pointer first:

Note that the pointer dereference must be enclosed in parenthesis, because the member selection operator has a higher precedence than the dereference operator.

Because the syntax for access to structs and class members through a pointer is awkward, C++ offers a second member selection operator (->) for doing member selection from pointers. The following two lines are equivalent:

This is not only easier to type, but is also much less prone to error because the dereference is implicitly done for you, so there are no precedence issues to worry about. Consequently, when doing member access through a pointer, always use operator-> instead of operator.

6.13 -- Void pointers
8.3 -- L-value references

13 comments to 6.12 — Member selection with pointers and references


    i love you Alex.
    every time i stuck and want to understand some thing deeply,i just come here... and i am sure i will get what i want.
    you must write all those heavy books.
    not these stupids called writers that just make things hazier!

  • dice3000

    Brain overflow. Need to take a rest :)

  • Vishal Singhal

    What cannot be done using references in C++. Can we have array of references just like array of pointers?

  • Pierre

    I don't understand how

    1.*pnValue = 6;
    2.rnValue = 6;

    if nValue started out as 5.

  • LOLY

    just question :

    is the code true ?

    int returnLocal(){
    int x=8 ;
    return ++x;

    int main() {
    int a=returnLocal() ;
    return 0;}

  • almalki

    thank you alex for evry think about this point .

    I hope to be your friend.

    sorry, i can not write english 100% , becouse i have mistakes.

    thanks for all friends

  • John Durant

    Here is what I am trying to do:

    Dynamically size an array
    Pass a reference to that array to a function
    Modify the size and or contents of the array
    Return the reference to the modified array

    I know this is possible using vectors, is it possible to do with arrays?


    • Yes, it is possible, just much more difficult than using vectors.

      When you say "dynamically size an array", do you want to dynamically allocate an array to a particular size, or dynamically resize an existing array? Either thing is possible, but the latter is much more difficult.

      Also, if you're passing a non-const reference to an array to a function, that means the function is going to modify the array. There's no need to return a reference back to the caller because the caller can access the array through it's original name.

  • You didn't end the struct.

    [ That is one my very least favorite things about C/C++. It's so easy to forget, and then you get weird error messages that don't really indicate the problem. Thanks for letting me know. -Alex ]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">