C++学习笔记---this指针

本文从C++的this指针出发,通过实例解释了this指针在对象成员函数调用中的作用,揭示了它如何确保正确访问对象属性。同时,文章探讨了const关键字的三种使用情况,帮助读者理解const如何限制指针的修改行为。
摘要由CSDN通过智能技术生成

一.引言

先从c语言结构体初始化的例子来说明this指针。定义一个结构体,并对其实例进行初始化。

#include<iostream>
#include<string.h>
using namespace std;
struct Student
{
	char name[10];
	int age;
	char sex[2];
};
void InitStudent(<fstruct Student* st char name[], int age, char sex[])
{
	strcpy(st->name, name);
	st->age = age;
	strcpy(st->sex, sex);
}
main()
{
	struct Student s1, s2;
	InitStudent(&s1, "张三", 12, "男");
	InitStudent(&s2, "李四", 14, "男");
}

在Initstudent()函数中,定义了结构体指针,在调用该函数时,传入对应结构体变量的地址,完成对该变量的初始化。

二.this指针

先看下面的代码:

#include<iostream>
#include<string.h>
using namespace std;
class CGoods
{
public:
	void RegisterGoods(char name[], int amount, float price);
	
	void CountTotal();
	
	void GetName(char name[]);

private:
	char Name[10];
	int Amount;
	float Price;
	float 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值