https://rentry.org/PPP2_p281a

// Code derived from Stroustrup's PPP2 book
// § 8.5.5 Pass-by-reference
//  -and beginning on p 281

#include <iostream>

using namespace std;

void swap(double& d1, double& d2)
{
  double temp = d1;    // copy d1's value to temp
  d1          = d2;    // copy d2's value to d2
  d2          = temp;  // copy d1's old value to d2
}

int main()
{
  double x = 1;
  double y = 2;

  cout << "x == " << x << ",  y == " << y << '\n';  // print: x == 1,  y == 2
  swap(x, y);
  cout << "x == " << x << ",  y == " << y << '\n';  // print: x == 2,  y == 1
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p281a.cpp && ./a.out

PrevUpNext

Edit
Pub: 04 Apr 2023 14:55 UTC
Edit: 03 May 2023 00:34 UTC
Views: 610