C++ Primer 第七章 Screen类与Window_mgr类

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值