/**************************************************************** reference.cpp some examples illustrating reference types ****************************************************************/ #include <iostream> using namespace std; void times2(int i) { // pass by copy, no return value cout << "void times2(int i) before: " << i << endl; i *= 2; cout << "void times2(int i) after: " << i << endl; } /* void times2(int& i) { // pass by reference, no return value cout << "void times2(int i) before: " << i << endl; i *= 2; cout << "void times2(int i) after: " << i << endl; } */ /* int times2(int i) { // pass by copy, return by copy cout << "int times2(int i) before: " << i << endl; i *= 2; cout << "int times2(int i) after: " << i << endl; return i; } */ /* int times2(int& i) { // pass by reference, return by copy cout << "int times2(int& i) before: " << i << endl; i *= 2; cout << "int times2(int& i) after: " << i << endl; return i; } */ /* //the following will NEVER compile int& times2(int i) { // pass by copy, return by reference cout << "int& times2(int i) before: " << i << endl; i *= 2; cout << "int& times2(int i) after: " << i << endl; return i; } */ /* int& times2(int& i) { // pass by reference, return by reference cout << "int& times2(int& i) before: " << i << endl; i *= 2; cout << "int& times2(int& i) after: " << i << endl; return i; } */ void main() { int test = 3; cout << "Before call: " << test << endl; //times2(test); //cout << "Result of call: " << times2(test) << endl; cout << "After call: " << test << endl; }