7.3.2. Functions That Return *this

Image

Next we’ll add functions to set the character at the cursor or at a given location:

class Screen {public:    Screen &set(char);    Screen &set(pos, pos, char);    // other members as before};inline Screen &Screen::set(char c){    contents[cursor] = c; // set the new value at the current cursor location    return *this;         // return this object as an lvalue}inline Screen &Screen::set(pos r, pos col, char ch){    contents[r*width + col] = ch;  // set specified location to given value    return *this;                  // return this object as an lvalue}

Like the move operation, our set members return a reference ...

Get C++ Primer, Fifth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.