zoukankan      html  css  js  c++  java
  • the pointer this

    The First Column The Second Column
    attribute [e'tribute]n.属性
    choose start menu folder 选择“开始”菜单文件夹
    expired 期满的
    expire 期满
    scratch 擦,刮,adj,打草稿用的,随便写的
    
    #include <iostream>
    using namespace std;
    
    class Student {
    public:
    	void setname(char *name);
    	void setage(int age);
    	void setscore(float score);
    	void show();
    private:
    	char *name;
    	int age;
    	float score;
    };
    
    void Student::setname(char *name) {
    	this->name = name;
    }
    void Student::setage(int age) {
    	this->age = age;
    }
    void Student::setscore(float score) {
    	this->score = score;
    }
    void Student::show() {
    	cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl;
    }
    
    int main() {
    	Student *pstu = new Student;
    	pstu->setname("李华");
    	pstu->setage(16);
    	pstu->setscore(96.5);
    	pstu->show();
    
    	return 0;
    }
    

    The Result:李华的年龄是16,成绩是96.5

    • This can only be used inside the class,and all members of the calss can be accessed through this,including private,protected and public attributes.
    • In this example,the name of the member functions' parameters are the same as those of member functions' variables,It can only be distinguished by this.
    • Take the member function setname(char* name) for example,its parameter is name,and the member variable name is named,if you write name=name;such a statement is to assign a parameter to the parameter name,instead of assigning the member variable name.And writing this -> name=name;,the name on the left is the member variable,and the name on the right is the parameter,which is clear at a glance.
    • This is a pointer to access member variables or member functions with ->
    • This is used whthin the class,but only when the object is created will it be assigned to this,and the process of the assignment is done automatically by the compiler,without user intervention,and the user can not explicitly assign a value to the this.In this this case,the value of this is the same as that of pstu.

    Adding a member function(printThis()) in class Student to specially print the value of this:

    void Student::printThis(){
    	cout<<this<<endl;
    }
    

    then create an object in the main() function and call printThis():

    #include <iostream>
    using namespace std;
    
    class Student {
    public:
    	void setname(char *name);
    	void setage(int age);
    	void setscore(float score);
    	void show();
    	void printThis();
    private:
    	char *name;
    	int age;
    	float score;
    };
    
    void Student::setname(char *name) {
    	this->name = name;
    }
    void Student::setage(int age) {
    	this->age = age;
    }
    void Student::setscore(float score) {
    	this->score = score;
    }
    void Student::show() {
    	cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl;
    }
    void Student::printThis() {
    	cout << this << endl;
    }
    int main() {
    	Student *pstu = new Student;
    	pstu->setname("李华");
    	pstu->setage(16);
    	pstu->setscore(96.5);
    	pstu->show();
    	
    	Student *pstu1 = new Student;
    	pstu1->printThis();
    	cout << pstu1 << endl;
    	
    	Student *pstu2 = new Student;
    	pstu2->printThis();
    	cout << pstu2 << endl;
    
    	return 0;
    }
    
    • The result:
    李华年龄是16,成绩是96.5
    0000026391651120
    0000026391651120
    0000026391651580
    0000026391651580
    

    It can be found that this does point to the current object,and for different objects,the value of this is different.

    A few notes:

    • This is const pointer,its value can not be modified,all attempts to modify the pointer's operation,such as assignment,increment,decrement and so on,are not allowed.
    • 'This' can only be used inside the memeber function,and it is illegal to use it elsewhere.
    • 'This' is meaningful only when the object is created,so it can not be used in the static member function(followed by static members).
  • 相关阅读:
    我的安卓开始
    OLAP的一些知识——接下去的项目需要的背景
    关于Java接口
    Hexo+Github/Coding免费搭建个人博客网站
    手机自带输入法emoji表情的输入,提交及显示——前端解决方案
    改变函数中的 this 指向——神奇的call,apply和bind及其应用
    什么是jsonp?——使用jsonp解决跨域请求问题
    玩转angularJs——通过自定义ng-model,不仅仅只是input可以实现双向数据绑定
    利用jquery mobiscroll插件选择日期、select、treeList的具体运用
    转载:中年程序猿的迷茫,你还在深究技术吗?
  • 原文地址:https://www.cnblogs.com/hugeng007/p/9335270.html
Copyright © 2011-2022 走看看