this指针

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值