References
The reference is the variable that stores the address of certain data. References have many familiar features with pointers. Here I will show how references work and the difference between them and pointers. Let’s start by creating. To declare the reference use the ampersand &:
int a = 3;
int *pa = &a;
int &ra = a;
The reference also has its name and type, but it doesn’t need to get the address of the variable using the additional operator &. Nevertheless, in both cases, we assign the address of the variable a to another variable (pointer or reference):
If we want to print created pointer and reference, we will get the following:
12cout << pa << endl; cout << ra << endl;
By outputting the reference, we got the value it stores since references immediately work with the value they point to. You cannot just add the asterisk * to the reference (like with pointers) to get the address, as such syntax will cause an error.
Pay attention that you cannot declare the reference without the variable it will point to.
int a = 3;
int *pa = &a;
int &ra = a;
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Fragen Sie mich Fragen zu diesem Thema
Zusammenfassen Sie dieses Kapitel
Zeige reale Beispiele
Awesome!
Completion rate improved to 2.94
References
Swipe um das Menü anzuzeigen
The reference is the variable that stores the address of certain data. References have many familiar features with pointers. Here I will show how references work and the difference between them and pointers. Let’s start by creating. To declare the reference use the ampersand &:
int a = 3;
int *pa = &a;
int &ra = a;
The reference also has its name and type, but it doesn’t need to get the address of the variable using the additional operator &. Nevertheless, in both cases, we assign the address of the variable a to another variable (pointer or reference):
If we want to print created pointer and reference, we will get the following:
12cout << pa << endl; cout << ra << endl;
By outputting the reference, we got the value it stores since references immediately work with the value they point to. You cannot just add the asterisk * to the reference (like with pointers) to get the address, as such syntax will cause an error.
Pay attention that you cannot declare the reference without the variable it will point to.
int a = 3;
int *pa = &a;
int &ra = a;
Danke für Ihr Feedback!