Last active
December 13, 2023 23:17
-
-
Save mshafae/880c7bba03f62a3cf7e6a16321aacaf6 to your computer and use it in GitHub Desktop.
CPSC 120 Example showing how references are different than copies of a value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gist https://gist.github.com/880c7bba03f62a3cf7e6a16321aacaf6 | |
#include <iostream> | |
int main(int argc, char const* argv[]) { | |
int number{42}; | |
int& number_reference = number; | |
int a_copy{number}; | |
int another_copy{-23}; | |
std::cout << "number: " << number << "\n"; | |
std::cout << "number_reference: " << number_reference << "\n"; | |
std::cout << "a_copy: " << a_copy << "\n"; | |
std::cout << "another_copy: " << another_copy << "\n"; | |
std::cout << "Let's change number and make another copy.\n"; | |
another_copy = number; | |
number = 17; | |
std::cout << "number: " << number << "\n"; | |
std::cout << "number_reference: " << number_reference << "\n"; | |
std::cout << "a_copy: " << a_copy << "\n"; | |
std::cout << "another_copy: " << another_copy << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment