example5_ref_const.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>

int main() {
    int x = 5;
    const int& ref_x = x;

    auto y = ref_x;    // y is an int (copy of x)
    auto& z = ref_x;   // z is a const int& (reference to x)

    std::cout << "y: " << y << std::endl;  // Outputs: y: 5
    std::cout << "z: " << z << std::endl;  // Outputs: z: 5

    return 0;
}
Back to auto