1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
void swap_val(int, int);
void swap_ptr(int*, int*);
void swap_ref(int&, int&);
int main()
{
int x { 5 };
int y { 205 };
std::cout << "Main: before swap x: " << x << ", y: " << y << "\n\n";
swap_val(x, y);
std::cout << "Main: after swap_val x: " << x << ", y: " << y << "\n\n";
swap_ptr(&x, &y);
std::cout << "After swap_ptr x: " << x << ", y: " << y << "\n\n";
swap_ref(x, y);
std::cout << "After swap_ref x: " << x << ", y: " << y << '\n';
}
void swap_val(int x, int y)
{
std::cout << "\tSwap_val. Before swap, x: " << x << " y: " << y << '\n';
int temp { x };
x = y;
y = temp;
std::cout << "\tSwap_val. After swap, x: " << x << " y: " << y << "\n\n";
}
void swap_ptr(int* px, int* py)
{
std::cout << "\tSwap_ptr. Before swap, *px: " << *px << ", *py: " << *py << '\n';
int temp { *px };
*px = *py;
*py = temp;
std::cout << "\tSwap_ptr. After swap, *px: " << *px << ", *py: " << *py << "\n\n";
}
void swap_ref(int& rx, int& ry)
{
int temp { rx };
std::cout << "\tSwap_ref. Before swap, rx: " << rx << ", ry: " << ry << '\n';
rx = ry;
ry = temp;
std::cout << "\tSwap_ref. After swap, rx: " << rx << ", ry: " << ry << "\n\n";
}
|