• jQuery火箭图标返回顶部代码


    this指针的相关概念:

              this只能在成员函数中使用。全局函数,静态函数都不能使用this。实际上,成员函数默认第一个参数为T* const register this。

              为什么this指针不能再静态函数中使用?

              大家可以这样理解,静态函数如同静态变量一样,他不属于具体的哪一个对象,静态函数表示了整个类范围意义上的信息,而this指针却实实在在的对应一个对象,

               所以this指针当然不能被静态函数使用了,同理,全局函数也一样,我是这样理解的,不知道大家怎样理解,大家可以评论交流下。

    (1)this指针是什么时候创建的?

                this在成员函数的开始执行前构造的,在成员的执行结束后清除。

    (2)this指针如何传递给类中函数的?绑定?还是在函数参数的首参数就是this指针.那么this指针又是如何找到类实例后函数的?

                this是通过函数参数的首参数来传递的。this指针是在调用之前生成的。类实例后的函数,没有这个说法。类在实例化时,只分配类中的变量空间,并没有为函数分配空间。

                自从类的函数定义完成后,它就在那儿,不会跑的。

    (3)this指针只有在成员函数中才有定义。因此,你获得一个对象后,也不能通过对象使用this指针。所以,我们也无法知道一个对象的this指针的位置(只有在成员函数

                里才有this指针的位置)。当然,在成员函数里,你是可以知道this指针的位置的(可以&this获得),也可以直接使用的。

    this指针的使用:

           一种情况就是,在类的非静态成员函数中返回类对象本身的时候,我们可以使用圆点运算符(*this).,箭头运算符this->,另外,我们也可以返回

    关于*this的引用,这样我们可以像输入输出流那样进行“级联”操作。

    示例如下:

    1:(普通)

     1 #include "stdafx.h"
     2 #include<iostream>  
     3 #include<string>  
     4 using namespace std;
     5 class Stu_Info_Mange
     6 {
     7     int sno;
     8     string sname;
     9     int age;
    10     int grade;
    11 public:
    12     Stu_Info_Mange(int s = 0, string n = "none", int a = 0, int g = 0)
    13     {
    14         sno = s;
    15         sname = n;
    16         age = a;
    17         grade = g;
    18     }
    19     void Setsname(std::string sn)   //使用this指针进行赋值  
    20     {
    21         this->sname = sn;
    22     }
    23     int  Setage(int a)
    24     {
    25         this->age = a;
    26         return (*this).age; //使用this指针返回该对象的年龄  
    27     }
    28     void show()
    29     {
    30         cout << "the sname is " << this->sname << endl;  //显式this指针通过箭头操作符访问  
    31         cout << "the sno   is " << sno << endl;//隐式使用this指针打印  
    32         cout << "the age   is " << (*this).age << endl;//显式使用this指针通过远点操作符  
    33         cout << "the grade is " << this->grade << endl << endl;
    34     }
    35 
    36 };
    37 int main()
    38 {
    39     Stu_Info_Mange p1(761, "张三", 19, 3);
    40     p1.show();  //输出信息 
    41     p1.Setage(12); //使用this指针修改年龄 
    42     p1.Setsname("王五");
    43     p1.show();     //再次输出  
    44     return 0;
    45 }

    2:(使用类)

    Stu_Info_Mange.h

     1 #pragma once
     2 #include<string>
     3 using std::string;
     4 class Stu_Info_Mange
     5 {
     6 private:
     7     int sno;
     8     string sname;
     9     int age;
    10     int grade;
    11 public:
    12     Stu_Info_Mange(int s = 0, string n = "none", int a = 0, int g = 0);
    13     void Setsname(string sn) { this->sname = sn; }
    14     int  Setage(int a);
    15     void show()const;
    16     ~Stu_Info_Mange();
    17 };

    Stu_Info_Mange.cpp

     1 #include "stdafx.h"
     2 #include "Stu_Info_Mange.h"
     3 #include<iostream>
     4 #include<string>
     5 using namespace std;
     6 Stu_Info_Mange::Stu_Info_Mange(int s, string n, int a, int g)
     7 {
     8     sno = s;
     9     sname = n;
    10     age = a;
    11     grade = g;
    12 }
    13 
    14 int Stu_Info_Mange::Setage(int a)
    15 {
    16     this->age = a;
    17     return (*this).age;
    18 }
    19 
    20 void Stu_Info_Mange::show() const
    21 {
    22     cout << "the sname is " << this->sname << endl;
    23     cout << "the sno   is " << sno << endl;
    24     cout << "the age   is " << (*this).age << endl;
    25     cout << "the grade is " << this->grade << endl << endl;
    26 }
    27 
    28 Stu_Info_Mange::~Stu_Info_Mange()
    29 {
    30 }

    ConsoleApplication.cpp

     1 #include "stdafx.h"
     2 #include "Stu_Info_Mange.h"
     3 #include<iostream>
     4 #include<string>
     5 using namespace std;
     6 int main()
     7 {
     8     Stu_Info_Mange p1(761, "张三", 19, 3);
     9     p1.show();  //输出信息 
    10     p1.Setage(12); //使用this指针修改年龄 
    11     p1.Setsname("王五");
    12     p1.show();     //再次输出  
    13     return 0;
    14 }

    输出结果:

  • 相关阅读:
    自己修改的两个js文件
    .net4缓存笔记
    使用.net的Cache框架快速实现Cache操作
    关于招聘面试(转)
    PHP中获取当前页面的完整URL
    Linux在本地使用yum安装软件(转)
    Phalcon的学习篇-phalcon和devtools的安装和设置
    GY的实验室
    aip接口中对url参数md5加密防篡改的原理
    nginx 多站点配置方法集合(转)
  • 原文地址:https://www.cnblogs.com/Trojan00/p/8894380.html
Copyright © 2020-2023  润新知