Cuando una variable se declara como referencia, se convierte en un nombre alternativo para una variable existente. Una variable se puede declarar como referencia poniendo ‘&’ en la declaración.
CPP
#include <iostream> using namespace std; int main() { int x = 10; // ref is a reference to x. int& ref = x; // Value of x is now changed to 20 ref = 20; cout << "x = " << x << '\n'; // Value of x is now changed to 30 x = 30; cout << "ref = " << ref << '\n'; return 0; }
Producción:
CPP
#include <iostream> using namespace std; void swap(int& first, int& second) { int temp = first; first = second; second = temp; } int main() { int a = 2, b = 3; swap(a, b); cout << a << " " << b; return 0; }
CPP
struct Student { string name; string address; int rollNo; } // If we remove & in below function, a new // copy of the student object is created. // We use const to avoid accidental updates // in the function as the purpose of the function // is to print s only. void print(const Student &s) { cout << s.name << " " << s.address << " " << s.rollNo << '\n'; }
CPP
#include <bits/stdc++.h> using namespace std; int main() { vector<int> vect{ 10, 20, 30, 40 }; // We can modify elements if we // use reference for (int& x : vect) { x = x + 5; } // Printing elements for (int x : vect) { cout << x << " "; } cout << '\n'; return 0; }
CPP
#include <bits/stdc++.h> using namespace std; int main() { vector<string> vect{ "geeksforgeeks practice", "geeksforgeeks write", "geeksforgeeks ide" }; // We avoid copy of the whole string // object by using reference. for (const auto& x : vect) { cout << x << '\n'; } return 0; }
C++
#include <iostream> using namespace std; int main() { int i = 10; // simple or ordinary variable. int* p = &i; // single pointer int** pt = &p; // double pointer int*** ptr = &pt; // triple pointer // All the above pointers differ in the value they store // or point to. cout << "i = " << i << "\t" << "p = " << p << "\t" << "pt = " << pt << "\t" << "ptr = " << ptr << '\n'; int a = 5; // simple or ordinary variable int& S = a; int& S0 = S; int& S1 = S0; cout << "a = " << a << "\t" << "S = " << S << "\t" << "S0 = " << S0 << "\t" << "S1 = " << S1 << '\n'; // All the above references do not differ in their // values as they all refer to the same variable. }
CPP
#include <iostream> using namespace std; int& fun() { static int x = 10; return x; } int main() { fun() = 30; cout << fun(); return 0; }
CPP
#include <iostream> using namespace std; int fun(int& x) { return x; } int main() { cout << fun(10); return 0; }
CPP
#include <iostream> using namespace std; void swap(char*& str1, char*& str2) { char* temp = str1; str1 = str2; str2 = temp; } int main() { char* str1 = "GEEKS"; char* str2 = "FOR GEEKS"; swap(str1, str2); cout << "str1 is " << str1 << '\n'; cout << "str2 is " << str2 << '\n'; return 0; }
CPP
#include <iostream> using namespace std; int main() { int x = 10; int* ptr = &x; int&* ptr1 = ptr; }
CPP
#include <iostream> using namespace std; int main() { int* ptr = NULL; int& ref = *ptr; cout << ref << '\n'; }
CPP
#include <iostream> using namespace std; int& fun() { int x = 10; return x; } int main() { fun() = 30; cout << fun(); return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA