Reference in c++

GOCOURSE
Reference in c++


Reference:

A reference in C++ is an alias or a new name for an existing variable. A way to access a variable without actually copying its value is known as this. The declaration uses the ampersand symbol (&) to indicate a reference.

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int& y = x; // y is a reference to x

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;

    y = 20; // changes the value of x

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;

    return 0;
}

Output:

x = 10
y = 10
x = 20
y = 20


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send