C++中的this指针

用法一

Human::Human(int age, int salary) {
	
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";
}
...
Human h1(21, 5000);

谁调用指向谁,这里this指向h1对象。

用法二

#include <iostream>
using namespace std;

class Human {
public:
	Human(string name, int age, int salary);
	int getAge() const;
	const Human* compare(const Human*);

private:
	string name;
	int age;
	int salary;
	char *addr;
};

int Human::getAge() const {
	return age;
}

const Human* Human::compare(const Human* other) {
	if (age > other->age) {
		return this;
	}
	else {
		return other;
	}
}

Human::Human(string name, int age, int salary) {
	cout << "调用自定义构造函数" << endl;
	this->name = name;
	this->age = age;
	this->salary = salary;
	
	addr = new char[16];
	strcpy_s(addr, 16, "China");
}

int main() {
	
	Human h1("Andy", 28, 2000);
	Human h2("Van", 23, 4500);
	cout << h1.compare(&h2)->getAge() << endl;

	return 0;
}

结果:28

用法三

#include <iostream>
using namespace std;

class Human {
public:
	Human(string name, int age, int salary);
	int getAge() const;
	const Human& compare(const Human&);

private:
	string name;
	int age;
	int salary;
	char *addr;
};

int Human::getAge() const {
	return age;
}

const Human& Human::compare(const Human& other) {
	if (age > other.age) {
		return *this;
	}
	else {
		return other;
	}
}

Human::Human(string name, int age, int salary) {
	cout << "调用自定义构造函数" << endl;
	this->name = name;
	this->age = age;
	this->salary = salary;
	
	addr = new char[16];
	strcpy_s(addr, 16, "China");
}

int main() {
	
	Human h1("Andy", 28, 2000);
	Human h2("Van", 23, 4500);
	cout << h1.compare(h2).getAge() << endl;

	return 0;
}

### 关于C++中`this`指针的概念 在C++中,每一个类的对象都可以通过一个特殊的隐含指针——`this`指针访问其自身的地址。该指针是一个指向当前调用成员函数的对象的常量指针[^4]。具体来说,`this`指针作为隐式参数传递给所有的非静态成员函数。 当某个对象调用它的成员函数时,编译器会自动将该对象的地址作为`this`指针传入到这个成员函数中。这使得成员函数能够区分局部变量和类的数据成员,并允许对特定实例的操作进行明确控制。 --- ### `this`指针的主要用途 #### 1. **解决命名冲突** 如果成员函数的形参名称与数据成员相同,则可以通过`this->`来显式指定操作的是类的数据成员而非局部变量。 ```cpp class Example { private: int value; public: Example(int value) : value(value) {} // 使用初始化列表避免歧义 void setValue(int value) { this->value = value; // 明确表示赋值给数据成员 } }; ``` 在这里,`setValue`方法中的`value`既是形参也是数据成员的名字。为了消除二义性,使用了`this->value`来特指类的数据成员[^3]。 --- #### 2. **返回当前对象本身** 有时需要在一个成员函数内部返回当前对象以便实现链式调用(chaining calls)。此时可利用`this`指针完成这一目标: ```cpp class Counter { private: int count; public: Counter() : count(0) {} Counter& increment() { ++count; return *this; // 返回当前对象的引用 } void displayCount() const { std::cout << "Current Count: " << count << '\n'; } }; // 链式调用示例: Counter c; c.increment().increment().displayCount(); ``` 上述例子展示了如何借助`*this`让多个连续的方法调用成为可能[^4]。 --- #### 3. **用于构造函数中的委托** 虽然严格意义上讲这不是直接应用`this`指针的地方,但在某些场景下可以用类似的逻辑处理不同类型的构造过程。不过更常见的情况还是前面提到的那种形式化表达方式。 注意:对于拷贝构造函数而言,默认情况下不会自动生成合适的版本;因此如果你希望支持深复制等功能的话就需要手动编写相应的代码片段并合理运用`this`机制辅助完成这些任务。 --- ### 示例程序解析 下面给出一段综合性的示范代码及其逐行解读说明如下所示: ```cpp #include <iostream> using namespace std; class Point { private: double x, y; public: // 构造函数 Point(double xx=0.0,double yy=0.0):x(xx),y(yy){ cout<<"Constructor Called"<<endl; } // 移动点位置 void moveByOffset(double dx,double dy){ (*this).x +=dx;// 或者写成 this->x +=dx ; (*this).y +=dy; } // 打印坐标信息 void showCoordinates(){ cout<< "("<<(*this).x<<","<<(*this).y<<")\n"; } // 测试是否相等 (基于距离原点远近判断) bool equals(const Point &other)const{ return ((fabs(this->x-other.x)<EPSILON)&&(fabs(this->y-other.y)<EPSILON)); } static constexpr double EPSILON = 1e-9 ; // 定义一个小数值用来比较浮点数精度差异忽略不计范围内的误差容忍度设定阈值。 }; int main(){ Point p1(3.5,-7.8); p1.showCoordinates(); p1.moveByOffset(-1,+2); p1.showCoordinates(); Point p2=p1; if(p1.equals(p2)){ cout<<"Points are equal.\n"; }else{ cout<<"Points differ.\n"; } return 0;} ``` 在这个案例里头包含了几个典型的应用场合比如修改属性值以及判定两个实体之间是否存在某种意义上的相似关系等等都体现了`this`关键字的重要性所在之处[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

run sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值