I'm back to doing some C++ work after 30 years or so (I was always more a C and later Objective-C person and haven't programmed in any C-like language for quite a while). And while I get things working, there is something in C++ that I cannot make sense of in my (C?) thinking. It is about this (simplified):
string line;
getline( line);
The getline function's argument is of type string&
, i.e. declaration:
bool getline( string &str);
Now it is clear that the line object needs to pass 'by reference' to the getline() function. After all, we want to fill our line object and not some throwaway copy. So, my intuition says I need to say:
getline( &line);
In C I would expect something like:
struct line; // contains the array, kind of C++ without member functions
getline( &line);
and the function as
getline( struct line*);
The question is: how does an seemingly by-value call of getline( line)
turn into the actual by-reference passing of the object? What happens in the background?
My guess is that a definition like
string line;
actually creates a pointer (line) of a different type than the * type.
I think what confuses me is that the call description string &
(with &) is different from the definition string line;
(without &). In C no such thing happens.
Can someone provide a good explanation to make me understand it in full?