文章目录
一.引言
先从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