一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果。this作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象本身的地址作为一个隐含参数传递给函数。
this 是指向当前对象的指针,那个对象调用包含this指针的函数,this指向那个对象。
this一般在构造函数中使用,用来区分成员变量和参数。
this指针在函数调用的时候,指向的是对象;(由此修改对象中的成员变量)
#include<iostream>
using namespace std;
class people {
int a;
public:
int b;
void fun(int b) {
this->b//成员变量 = b;//参数
this指针可以用来区分成员变量和参数
}
};
int main() {
people b;
b.fun(10);
}
#include<istream>
class people{
double weight;
double wealth;
string name;
public:
void get_weight(double weight){
return weight;
}
void set_weight(double weight){
if(0<weight&&weight<300){
this->weight=weight;
}
}
};
int main{
people p;
p.set_weight(3);
p.get_weight();
}