注册 登录
编程论坛 C++教室

返回*this的问题?

newyj 发布于 2008-03-21 13:52, 692 次点击
例:
screen& screen::set(char c){
  contents[cursor]=c;
  return *this;
}
中的return *this返回的是什么啊
3 回复
#2
eagleboycn2008-03-21 14:08
返回调用该函数 ( screen& screen::set(char c) )
的screen对象
#3
zjl1382008-03-21 14:09
this就是指向你当前代码所在的对象。等价于return contests[cursor];
this指针的使用:
一种情况就是,在类的非静态成员函数中返回类对象本身的时候,直接使用 return *this;
另外一种情况是当参数与成员变量名相同时,如this->n = n (不能写成n = n)。
#4
newyj2008-03-21 19:49
谢谢楼上两位的解答
screen& screen::move(inde r,index c){
  index row=r*width;
  cursor=row+c;
  return *this;
}
那这个*this返回的是cursor吗
这里有两个对象啊

[[it] 本帖最后由 newyj 于 2008-3-21 20:21 编辑 [/it]]
1