Following program output is correct but there comes a error when I use this in place of *this.Can anybody tell me what this and *this means
#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;
}
Output:
1 2 3 4
When I use this in place of *this error comes:
In member function 'Test& Test::fun()':
invalid initialization of non-const reference of type 'Test&' from an rvalue of type 'Test*'
return this;
Can anybody tell me what is the difference between this and *this by giving a good example?