Cpp - Reassign value via reference

Description

Reassign value via reference

Demo

#include <iostream> 
 
int main() /* ww  w .j a v a  2s .  c  om*/
{ 
    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