C++ Language base this and static keywords

  • 2020-06-23 01:32:32
  • OfStack

1. this keyword

this is a pointer that you can access a member variable or a member function

Here is a complete example of using this:


#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<<" The age is "<<this->age<<" , the result is "<<this->score<<endl;
}

int main(){
  Student *pstu = new Student;
  pstu -> setname(" Li hua ");
  pstu -> setage(16);
  pstu -> setscore(96.5);
  pstu -> show();

  return 0;
}

Operation results:

[

Li Hua's age is 16 and her grade is 96.5

]

this can only be used inside the class, and all members of the class, including private, protected, public attributes, can be accessed through this.

In this case, the parameter and member variable names of the member functions can only be distinguished by this. Take the member function setname(char *name) as an example. Its formal parameter is name, and the member variable name has the same name. Such a statement assigns a value to the parameter name, not to the member variable name. this - > name = name; After, = name on the left is the member variable and name on the right is the formal parameter.

2. static keyword

2.1 static static member variables

Similar to java,C++ also has static static member variable, which is used as follows:


#include <iostream>

using namespace std;

class Student {
public:
  Student(char *name, int age, float score);
  void show();
public:
  static int m_total; //  Static member variable 
private:
  char *m_name;
  int m_age;
  float m_score;
};

int Student::m_total = 0; //  Static member variables need not be added when initialized static


Student::Student(char *name, int age, float score) {

}
void Student::show() {

}


int main()
{
  Student::m_total = 10; //  Can be accessed directly by the class name 
  //  The stack area 
  Student stu("Jack",15,92.5f);
  stu.m_total = 20;   //  It can also be accessed directly by the object name 
  //  The heap area 
  Student *pstu = new Student("Tom",16,96);
  pstu->m_total = 20;  //  It can also be accessed directly by the object name 

  delete pstu;
  return 0;

}

Note:

1) A class can have one or more static member variables that are Shared and referenced by all objects.

2) Like static member variable and ordinary static variable 1, the memory is allocated in the global data area of the memory partition and is not released until the end of the program. This means that the static member variable does not allocate memory with object creation or free memory with object destruction. Ordinary member variables allocate memory when an object is created and free it when it is destroyed.

3) Static member variables must be initialized and can only be done outside of the class. Such as:

[

int Student: : m_total = 0; // No static is needed to initialize static member variables

]

Initializers may or may not be assigned an initial value. If no value is assigned, it will be initialized to 0 by default. Variables in the global data area have a default initial value of 0, while the default value of variables in the dynamic data area (heap area, stack area) is uncertain.

4) Static member variables can be accessed by both object and class names, subject to the access restrictions of the private, protected and public keywords. When accessed by object name, the same amount of memory is accessed for different objects.

2.2 static static member function

The following is a demonstration of the static static member function:


#include <iostream>
using namespace std;

class Student{
public:
  Student(char *name, int age, float score);
  void show();
public: // Declare static member functions 
  static int getTotal();
  static float getPoints();
private:
  static int m_total; // The total number of 
  static float m_points; // Total grade 
private:
  char *m_name;
  int m_age;
  float m_score;
};

int Student::m_total = 0;
float Student::m_points = 0.0;

Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){
  m_total++;
  m_points += score;
}
void Student::show(){
  cout<<m_name<<" The age is "<<m_age<<" , the result is "<<m_score<<endl;
}
// Define static member functions 
int Student::getTotal(){
  return m_total;
}
float Student::getPoints(){
  return m_points;
}

int main(){
  (new Student(" Xiao Ming ", 15, 90.6)) -> show();
  (new Student(" Li lei ", 16, 80.5)) -> show();
  (new Student(" Zhang hua ", 16, 99.0)) -> show();
  (new Student(" Wang Kang ", 14, 60.8)) -> show();

  int total = Student::getTotal();
  float points = Student::getPoints();
  cout<<" The current total of "<<total<<" Student, the total score is "<<points<<" , the average score is "<<points/total<<endl;

  return 0;
}

Note:

1) The fundamental difference between static member functions and ordinary member functions is that ordinary member functions have this pointer, which can access any member in the class; Static member functions have no this pointer and can only access static members (both static member variables and static member functions).

2) getTotal() and getPoints() in the above example can also be declared as normal member functions, but they operate only on static members, plus static semantics is more explicit.

3) Similar to static member variables, static member functions are declared with static and defined without static. Static member functions can be called from a class (usually 1), but they can also be called from an object.


Related articles: