7.3类的其它特性
class Screen{
typedef string::size_type pos;
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
mutable size_t access_ctr;
void do_display(ostream &os) const {os << contents;}
public:
Screen() = default;
Screen(pos ht, pos wt) : height(ht), width(wt), contents(ht* wt, ' ') {}
Screen(pos ht, pos wt, char c) : height(ht), width(wt), contents(ht* wt, c) {}
char get () const {return contents[cursor];}
inline char get(pos ht, pos wt) const {return contents[ht* width + wt];}
Screen &move(pos r, pos c);
Screen &set(char c);
Screen &set(pos h, pos w, char c);
const Screen &display(ostream &os) const {do_display(os); return *this;};
Screen &display(ostream &os) {do_display(os); return *this;};
void number() const;
};
inline Screen& Screen::move(pos r, pos c)
{
pos row = r*width;
cursor = row + c;
return *this;
}
void Screen::number() const
{
access_ctr++;
}
Screen &Screen::set(char c){
contents[cursor] = c;
return *this;
}
Screen &Screen::set(pos h, pos w, char c){
contents[h*width + w] = c;
return *this;
}