Cpp - Reassign value via reference

Description

Reassign value via reference

Demo

#include <iostream> 
 
int main() /*from   w  w w .  ja  v a  2 s.c  o  m*/
{ 
    int intOne; 
    int &rSomeRef = intOne; 
 
    intOne = 5; 
    std::cout << "intOne:\t" << intOne << std::endl; 
    std::cout << "rSomeRef:\t" << rSomeRef << std::endl; 
    std::cout << "&intOne:\t"  << &intOne << std::endl; 
    std::cout << "&rSomeRef:\t" << &rSomeRef << std::endl; 
 
    int intTwo = 8; 
    rSomeRef = intTwo;
    std::cout << "\nintOne:\t" << intOne << std::endl; 
    std::cout << "intTwo:\t" << intTwo << std::endl; 
    std::cout << "rSomeRef:\t" << rSomeRef << std::endl; 
    std::cout << "&intOne:\t"  << &intOne << std::endl; 
    std::cout << "&intTwo:\t"  << &intTwo << std::endl; 
    std::cout << "&rSomeRef:\t" << &rSomeRef << std::endl; 
    return 0; 
}

Result

rSomeRef continues to act as an alias for intOne, so this assignment

rSomeRef = intTwo;

is exactly equivalent to the following:

intOne = intTwo; 

Related Topic