class Window_mgr;
class Screen
{
friend class Window_mgr;
public:
using pos = string::size_type;
//下面三个构造函数 隐式内联
Screen() = default;
Screen(pos he, pos wi) :height(he), width(wi), contents(he* wi, ' ') {}
Screen(pos he, pos wi, char c) :height(he), width(wi), contents(he* wi, c) {}
char get() const { return contents[cursor]; } //隐式内联
char get(pos, pos) const; //显示内联 一般推荐在类外定义处标明inline
Screen& move(pos, pos);
Screen& set(pos, pos, char);
Screen& set(char);
// 此处对于display函数进行基于const的重载原因是:display函数本身应该定义为const的,因为它不会对调用它的对象进行修改,但是返回值是引用类型的,若一个普通对象调用display函数,则返回const Screen&
// 这时就不合适了,因为例如s.display().move(3,3).get();这样的操作就非法了。所以对于display函数再进行基于const的重载,使非常量对象调用非常量版本的display函数,是合适的。
const Screen& display()const;
Screen& display();
private:
void do_display() const { cout << contents; };
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
inline const Screen
C++ Primer 第七章 Screen类与Window_mgr类
于 2022-05-13 23:08:32 首次发布