this指针

来源:互联网 发布:淘宝直播赚钱吗 编辑:程序博客网 时间:2024/05/18 01:26

this指针怎么来的?
代码一

#include<iostream>using namespace std;class student{                                  public:    void SetStuInfo(char* name, char*gender, int number)    {           cout << "this的内容为" <<this<< endl;        strcpy(_name, name);                     strcpy(_gender, gender);                _number = number;                }private:    char _name[4];    char _gender[2];    int _number;};int main(){    student s1;    student s2;    student s3;    s1.SetStuInfo("张三","男",20);    cout << "s1的地址为" << &s1 << endl;    cout << "类大小为" << sizeof(s1) << endl;    system("pause");    return 0;}用c语言来模拟 void SetStuInfo(char* name, char*gender, int number)函数;void SetStuInfo(student* p,char*name,char* gender,int number){        strcpy(p->_name, name);                     strcpy(p->_gender, gender);                p->_number = number; }相比较之下类内的void SetStuInfo()函数少了一个类对象那么问题就来了编译器是怎么知道哪个对象调用的它s1,s2,s3?这时候就有了this指针用来保存当前调用类函数的类对象的地址(以下代码可以说明)。``代码二#include<iostream>  using namespace std;  class student  {                                         //编译器所做如下  public:      //void SetStuInfo(student* const this, char* name, char*gender, int number)      void SetStuInfo(char* name, char*gender, int number)      {             cout << "this的内容为" <<this<< endl;          strcpy(_name, name);              //strcpy(this->_name, name);          strcpy(_gender, gender);          //strcpy(this->_gender, gender);          _number = number;                 //this->_number = number;      }  private:      char _name[4];      char _gender[2];      int _number;  };  int main()  {      student s1;      s1.SetStuInfo("张三","男",101);      cout << "s1的地址为" << &s1 << endl;      cout << "类大小为" << sizeof(s1) << endl;      system("pause");      return 0;  }   图一为以上代码结果图二为加了this指针后的结果代码三#include<iostream>using namespace std;class student{public:void SetStuInfo(char* name, char*gender, int number){cout << "this的内容为" << this << endl;strcpy(_name, name);strcpy(_gender, gender);_number = number;}private:char _name[4];char _gender[2];int _number;};void Test(){cout << "this的内容为" << this << endl;}int main(){student s1;s1.SetStuInfo("张三", "男", 20);cout << "this的内容为" << this << endl;cout << "s1的地址为" << &s1 << endl;cout << "类大小为" << sizeof(s1) << endl;system("pause");return 0;}错误:1>e:\vs文件\project\project\student.cpp(107): error C2440: “=”: 无法从“int”转换为“student *const1>从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换1>e:\vs文件\project\project\student.cpp(120): error C2355: “this”: 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用1>e:\vs文件\project\project\student.cpp(128): error C2355: “this”: 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用以上说明了this指针的特点     1.0  this指针:用来存放类对象的地址 或者说this指向类对象 2.0  this指针的作用域:类的成员函数里               3.0   this指针是类成员函数参数列表里的 第一个参数,不过是编译器自己在编译时加的,在类成员函数里每个成员变量都隐含由this指针指向。 4.0 sizeof(类对象)(求类的大小)的结果与this指针无关。 5.0   this指针类型>类类型  *const#include<iostream>using namespace std;class student{public:void SetStuInfo(...);private:char _name[4];char _gender[2];int _number;};int main(){student s1;s1.SetStuInfo("张三", "男", 20);system("pause");return 0;}1>student.obj : error LNK2019: 无法解析的外部符号 "public: void __cdecl student::SetStuInfo(...)" (?SetStuInfo@student@@QAAXZZ),该符号在函数 _main 中被引用以上代码和图三说明了__thiscall调用约定: 1.0  参数压入堆栈从右往左。 2.0  this指针([s1]表示s1的地址)通过寄存器ecx传递 3.0  __thiscall只能够用在类的成员函数上 4.0  如果参数个数确定,this指针通过ecx传递给被调用者;如果参数不确定(_cdecl),this指针在所有参数被压栈后压入堆栈。 问题:1.0 为什么叫this指针,而不是引用呢?         this指针存放目前调用对象的地址,引用则是与某个对象用共同一块内存,别名。 2.0 既然this是指针那么可不可以为NULL呢?看一段代码 #include<iostream>using namespace std;  class student  {     public:      void test1()      {            cout << this << endl;       }  private:      char _name[4];      char _gender[2];      int _number;  };  void test2()  {      student* s1 = NULL;      s1->test1();  }  int main()  {      test2();      system("pause");      return 0;  }

图四为以上程序结果说明this指针的值可以为NULL

这里写图片描述
图一
这里写图片描述
图二
这里写图片描述
图三
这里写图片描述
图四

原创粉丝点击