【发布时间】:2015-10-19 11:39:43
【问题描述】:
下面的程序输出是正确的,但是当我使用这个时出现错误 代替*this。谁能告诉我this和*this是什么意思
#include<iostream>
using namespace std;
class Test
{
private:
static int count;
public:
Test& fun(); // fun() is non-static now
};
int Test::count = 0;
Test& Test::fun()
{
Test::count++;
cout<<Test::count<<" ";
return *this;
}
int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
输出:
1 2 3 4
当我用这个代替*时,会出现这个错误:
In member function 'Test& Test::fun()':
invalid initialization of non-const reference of type 'Test&' from an rvalue of type 'Test*'
return this;
谁能告诉我this和*this有什么区别?
【问题讨论】: