class Window_mgr
{
public:
using ScreenIndex = vector<Screen>::size_type;
void clear(ScreenIndex);
private:
vector<Screen> screens{ Screen(24,80,' ') }; //一个Window_mgr包含一个标准尺寸的空白Screen
};
// 必须先声明Window_mgr 然后定义Screen 然后声明Window_mgr类的clear成员函数
class Screen
{
friend void Window_mgr::clear(ScreenIndex);
public:
using pos = string::size_type;
Screen() = default; //默认构造函数
Screen(pos h, pos w) :height(h), width(w), contents(h* w, ' ') {} //突然明白了这个初始化的方式。
Screen(pos h, pos w, char c) :height(h), width(w), contents(h* w, c){}
//cursor被类内初始化为0 如果类内不存在cursor的初始值,则必须显式地初始化cursor
public:
char get()const { return contents[cursor]; } //隐式内联
inline char get(pos h, pos w)const; //显示内联
Screen& move(pos h, pos w); //可以在之后被设为内联
Screen& set(pos, pos, char);
Screen& set(char);
Screen& display(ostream&os)
{
do_print(os); //this指针隐式地从指向非常量的指针转换为指向常量的指针
return *this; //this指向一个非常量的对象,因此display返回一个非常量的引用。
}
const Screen& display(ostream& os)const //常量成员函数 返回值是常对象
{
do_print(os);
return *this; //返回一个常量引用
}
private:
void do_print(ostream& os)const { os << contents; } //设置为引用的原因是ostream类对象不能拷贝复制
pos cursor = 0;
pos height = 0, width = 0;
string contents;
};
inline Screen& Screen::set(pos r, pos c, char ch)
{
pos row = r * width;
contents[row + c] = ch;
return *this;
}
inline Screen& Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen& Screen::move(pos r, pos c) //可以在函数的定义处指定inline
{
pos row = r * width; //计算行的位置
cursor = row + c; //在行内将光标移动到指定的列
return *this; //以左值的形式返回对象
}
char Screen::get(pos r, pos c) const //这个const必须加上,不然就是两个函数了
{ //在类内声明为inline
pos row = r * width;
return contents[row + c];
}
void Window_mgr::clear(ScreenIndex i)
{
Screen& tmp = screens[i];
tmp.contents = string(tmp.height*tmp.width, ' '); //string对象的初始化 string(int ,char)
}
int main()
{
Screen s1(12, 12, 'a');
cout << s1.get(1, 1) <<endl;
cout << s1.move(4, 4).set('z').get() << endl;
s1.display(cout);
cout << endl;
const Screen s2(2, 2, 'z');
s2.display(cout);
Screen s(3, 3, '#');
s.move(1,1).set('1').display(cout); // 这里的参数应该是下标类型
// ###
// ###
// ### ####1####
// 2,2 2*3+2
Screen myscreen(5, 5, 'X');
myscreen.set(4,0,'#').display(cout);
cout << endl;
myscreen.display(cout);
cout << endl;
return 0;
}
1.Screen类内定义类型别名也有public和private之分 若public,则类外可以通过类名定义别名的变量。
2.若定义了其他的构造函数,则需要定义一个默认构造函数
3.非默认构造函数中,如果数据成员没有类内初始值,则必须进行显式初始化,比如构造函数种cursor并没有显式初始化。
4.建议使用参数初始化列表,给数据成员初始化时建议使用参数,而非其他数据成员。
5.一开始没看懂这个contents是怎么初始化的,其实是string(10,'x')采取的是这种构造函数:string(int ,char)
6.定义在类内的成员函数默认指定为inline内联,定义在类外的也可以指定内联,建议在类外部定义的地方说明inline,且inline成员函数也应该与相应的类定义在同一个头文件中
7.如果一个成员函数只是进行读操作,则建议指定为常成员函数,即在形参列表后面加一个const,因为每一个成员函数中都有一个隐藏的this指针,如果是一个常对象调用一个普通的成员函数(非const的成员函数),相当于用一个普通的this指针(实际上是一个常量指针)指向了一个常对象,这是非法的。所以,当把某个只进行读操作的成员函数指定为常成员函数时,这个常成员函数的this指针就是const Screen* const this 的了,就可以被常对象调用了。
8.Screen类中的move和set成员函数返回值是Screen& ,而函数内部是return *this 相当于把调用这个成员函数的对象返回回去了,且返回回去的是一个左值,即可以进行链式操作。如:s1.move().set().display;
9.Screen类中的display函数,本身只进行读操作,应当定义为常成员函数,但是如果指定后,返回值是一个常对象,也就不能在同一条语句后面进行set等操作了。解决办法为:可以根据是否指定const,对成员函数进行重载。常对象会自动调用const版本的display函数,而普通的对象会自动调用非常属性的成员函数。 补:在普通的display函数中,常指针this在进行do_display时,会自动转换为指向常量的常指针。
10.ostream类对象指定为ostream&是因为此类对象不可进行拷贝赋值操作。不指定为const是因为这种对象在函数内的状态会被改变。。。
11.有关Window_mgr类:在定义Window_mgr类的clear成员函数时,有一定顺序要求:因为要将Window_mgr类或者它的成员函数clear指定为Screen类的友元,应该先定义Window_mgr类,在定义Screen类,再定义成员函数clear。
呃呃,我试了一下先声明Window_mgr,再定义Screen同时声明友元类Window_mgr,再定义Window_mgr,再定义clear。竟然可以。我记得之前不行啊 。晕眩了