references

In the book I am learning from it says.

A constant reference to a vector of sting objects named vec

 
  void display (const vector <string>& vec)


Does this mean the reference is named vec or the vector is called vec
The name of the variable representing the parameter is vec and it is of type reference-to-const vector<string>.
Im actually looking for the answer too, kmce, if you can answer your own question please tell me the answer, cires answer has confused me a little bit. thanks
This annotated example may help clear the confusion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>

void display( const std::vector<std::string>& vec )
{
    // the name of the variable (the formal parameter to the function) is 'vec'
    // the type of the variable is 'reference to const std::vector<std::string>'
    // each time the function is called, the variable 'vec' is created and initialised

    for( const auto& str : vec ) std::cout << std::quoted(str) << ' ' ;
    std::cout << '\n' ;

    // the variable 'vec' goes out of scope when the function returns
    // the next time the function is called, it is created and initialised afresh
}

int main()
{
    std::vector<std::string> vector_a { "zero", "one", "two", "three", "four" } ;
    // the name of the variable is 'vector_a'
    // the type of the variable is 'std::vector<std::string>'

    display(vector_a) ;
    // the semantics of passing an argument to a function is the semantics of initialisation.
    // 'vec' in the function display is seated on the object 'vector_a'

    std::vector<std::string> vector_b { "nihil", "unus", "duo", "tres", "quattuor" } ;
    // the name of the variable is 'vector_b'
    // the type of the variable is 'std::vector<std::string>'

    display(vector_b) ;
    // the semantics of passing an argument to a function is the semantics of initialisation.
    // 'vec' in the function display is initialised to refer to the object 'vector_b'
}

http://coliru.stacked-crooked.com/a/da7c12aae377e3d2
Sometimes people use phrasing like "the reference is the referent" or "a reference is like an 'automatic' pointer", which are strictly wrong, because they don't capture the whole picture.

If you want a phrase to explain references with, "a reference is an alias for its' referred-to-object (referent)" is alright. It's impossible to do anything with the reference itself - anything you try to do to the reference is done to the referent.

Still, "alias" doesn't capture the whole picture, because it doesn't explain dangling references nor references bound to an object whose referenced type differs from the referent's type, nor lifetime extension of temporaries which happen to be bound to a constant reference. (And maybe some other subtleties that I can't think of now.)
For these things, I don't know of any simple analogy. Still, you should be able to get by just fine with an analogy of your choice, as long as you know that it's simplified.

Maybe it would be better to just explain the rules without trying to teach references in one sentence. ;-)
Last edited on
Topic archived. No new replies allowed.