Reference types - Introducing
int num = 3;
int &refNum = num; //& tells us that refNum is a reference value;
//assign it to the num variable
//This binds refNum to num
//This reference cannot be reassigned later in program code
int &refNum2;//this line cause an error because it is not initilized
cout << refNum << endl; //result is 3, because refNum is an alias or reference to num, which
//holds the value 3 int num = 3;
int &refNum = num;
cout << "num contains " << num << endl;
cout << "refNum contains " << refNum << endl;
refNum++; // increment refNum by 1
// which will also increment num due to refNum bing an alias for num
cout << "num contains " << num << endl; //shows 4 as refNum
cout << "refNum contains " << refNum << endl; //output the memory address of num and refNum
//shows that they both point to the same memory location
//Hence, any changes made to refNum affect num
cout << "refNum is located at " << &refNum << " and num is located at " << &num << endl;Last updated