© Ray Lischner 2020
R. LischnerExploring C++20https://doi.org/10.1007/978-1-4842-5961-0_33

33. Assignment and Initialization

Ray Lischner1 
(1)
Ellicott City, MD, USA
 

The final step needed to complete this stage of the rational type is to write assignment operators and to improve the constructors. It turns out C++ does some work for you, but you often want to fine-tune that work. Let’s find out how.

Assignment Operator

Until now, all the rational operators have been free functions. The assignment operator is different. The C++ standard requires that it be a member function. One way to write this function is shown in Listing 33-1.
struct rational
{
  rational(int num, int den)
  : numerator{num}, denominator{den}
  {
    reduce();
  }
  rational& operator=(rational ...

Get Exploring C++20: The Programmer's Introduction to C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.