Why parameters passed by reference in C ++ do not require a dereference operator?

I am new to the C ++ community and just asking a quick question about how C ++ passes variables by reference to functions.

If you want to pass a variable by reference in C ++, you add & to any argument you want to pass by reference. How is it that when you assign a value to a variable that is passed by reference, why do you say variable = value; instead of saying *variable = value ?

 void add_five_to_variable(int &value) { // If passing by reference uses pointers, // then why wouldn't you say *value += 5? // Or does C++ do some behind the scene stuff here? value += 5; } int main() { int i = 1; add_five_to_variable(i); cout << i << endl; // i = 6 return 0; } 

If C ++ uses pointers to do this behind the scenes magic, why don't you need to play, as with pointers? Any insight would be greatly appreciated.

+4
source share
5 answers

When you write

 int *p = ...; *p = 3; 

This is the syntax for assigning 3 to the object pointed to by p . When you write

 int &r = ...; r = 3; 

This is the syntax for assigning 3 to the object referenced by r . The syntax and implementation are different. Links are executed using pointers (except when optimized), but the syntax is different.

So you can say that dereferencing happens automatically when necessary.

+5
source

C ++ uses pointers behind the scenes, but hides all this complication from you. Passing by reference also avoids all the problems associated with invalid pointers.

+2
source

When you pass an object to a function by reference, you control the object directly in the function without accessing its address, like pointers. Thus, when manipulating this variable, you do not want to cast it using the *variable syntax. This is a good practice for passing objects by reference, because:

  • Link cannot be redefined to point to another object
  • It cannot be null. you must pass a valid object of this type to a function

How the compiler achieves "pass by reference" does not really matter in your case.

Wikipedia article is a good resource.

+2
source

One has two questions:

  • one syntax question: difference between pointer and link
  • the other concerns mechanics and implementation: the representation in memory of a reference

Let me address the two separately.

Link and Pointer Syntax

A pointer conceptually means a “sign” (like a road sign) to an object. It allows you to perform 2 types of actions:

  • actions on pointee (or the object pointed to)
  • pointer action

operator* and operator-> allow you to access the pointer to distinguish it from your calls to the pointer itself.

The link is not a "sign", it is an alias. Throughout his life, hell or high water will come, he will point to the same object, and you can do nothing about it. Therefore, since you cannot access the link itself, there is no point bothering you with the strange syntax * or -> . Ironically, without using a strange syntax, it is called syntactic sugar.

Link mechanics

The C ++ standard does not talk about implementing links, it just hints that if the compiler succeeds in removing them. For example, in the following case:

 int main() { int a = 0; int& b = a; b = 1; return b; } 

A good compiler will understand that b is just a proxy for a , there is no room for doubt and, therefore, simply accesses a directly and optimizes b out.

As you may have guessed, the likely representation of a link is (under the hood) a pointer, but do not let it bother you, it does not affect the syntax or semantics. This means, however, that a number of pointer problems (for example, access to objects that have been deleted, for example) also affect links.

+1
source

Explicit dereferencing is not required by design - this is for convenience. When you use . by reference, the compiler emits the code needed to access the real object - this often involves dereferencing the pointer, but this is done without the need to explicitly dereference your code.

0
source

All Articles