2015-10-19 33 views
1

下面的程序输出是正确的,但我在的地方使用此 总会有这种错误* this.Can有人告诉我这是什么和*这意味着这和*这种差异

#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; 

可有人告诉我W¯¯通过给出一个很好的例子,帽子就是这个和这个*之间的区别吗?

+3

[什么是'this'指针?](http://stackoverflow.com/questions/16492736/what-is-the-this-pointer)和[指针是什么意思? ](http://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean) –

回答

7

谁能告诉我这是什么和*这意味着

this是指向当前对象。

*this是当前对象。

大家知道,推测是,解除引用操作符的作用是什么?我不知道你为什么试图交换this*this。根本不要那样做。