0

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?

gctwnl
  • 217
  • 1
  • 8
  • there is no "adress of" in this declaration: `bool getline( string &str);` . Same symbols can have different meanings – 463035818_is_not_an_ai Nov 18 '21 at 12:13
  • also you seem to be confused because you expect to be able to explain C++ with C, that wont work because C has no references – 463035818_is_not_an_ai Nov 18 '21 at 12:14
  • @463035818_is_not_a_number: Objects can be passed by reference in C; it is done manually by passing the address of an object instead of automatically. The terminology of passing things “by reference” in this way antedates C++’s adoption of the term “reference” to refer to a specific language feature. The C standard says that C’s pointers provide references to entities. – Eric Postpischil Nov 18 '21 at 12:20
  • That kind of pedantry is useless to helping anyone understand the behavior. – Cody Gray - on strike Nov 18 '21 at 12:21
  • C++ has a mechanism to pass objects by reference, and an argument with type `string &` is a reference to a `string`, so the caller passes the object by name (e.g. if a `string` named `x` is available to the caller, it would call `getline(x)`). C doesn't have that syntactic mechanism. In C, the argument would be a pointer (type `string *`) and the caller passes the address of an object. (e.g. if a string name `x` is available to the caller, it would call `getline(&x)`). – Peter Nov 18 '21 at 12:24
  • @CodyGray: It is not clear which comment you are responding to. When people versed in C use the terminology they use and people versed in C++ insist the C people are using their own terminology incorrectly, that is an impediment to communication. The C standard describes pointers as references, and people discussing references in C should be understand to be using the term that way. – Eric Postpischil Nov 18 '21 at 12:42

1 Answers1

2

When a function parameter is declared as a reference, as is string &str in bool getline(string &str), and the function is called, as with getline(line), the compiler passes the address of the argument to the function. That is, in the register or other location where an argument is passed to the function, the compiler puts the address of line instead of the value. (Technically, the compiler could use something other than the memory address, but it will be a functional equivalent.)

Effectively, declaring a parameter as a reference means “Automatically take the address of an argument and pass that, and, wherever the parameter is used in the function, automatically use the object that the passed address points to, as if *x had been used instead of x.”

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312