以下程序输出是正确的,但是当我使用它代替*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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1 2 3 4
Run Code Online (Sandbox Code Playgroud)
当我使用它代替*这个错误来:
In member function 'Test& Test::fun()':
invalid initialization of non-const reference of type 'Test&' from an rvalue of type 'Test*'
return this;
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这和*之间的区别是什么?
任何人都能告诉我这是什么意思和*这意味着什么
this 是指向当前对象的指针.
*this 是当前的对象.
大概是,您知道解除引用运算符的作用是什么?我不知道为什么你随意想换this的*this.根本不要那样做.