返回this的引用
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct obj {
struct obj& returnThis() {
return *this;
}
int data;
};
int main(void) {
struct obj myObject;
myObject.data = 100;
struct obj& Object = myObject.returnThis();
Object.data = 200;
cout << myObject.data << endl;
return 0;
}
返回this指针
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct obj {
struct obj& returnThis() {
return *this;
}
struct obj* returnthisP() {
return this;
}
int data;
};
int main(void) {
struct obj myObject;
myObject.data = 100;
struct obj& Object = myObject.returnThis();
Object.data = 200;
cout << myObject.data << endl;
struct obj* other = myObject.returnthisP();
other->data = 300;
cout << myObject.data << endl;
return 0;
}