Skip to content

Instantly share code, notes, and snippets.

@mshafae
Last active December 13, 2023 23:17
Show Gist options
  • Save mshafae/880c7bba03f62a3cf7e6a16321aacaf6 to your computer and use it in GitHub Desktop.
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.
// 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