Static modification of member variables and member functions of a class in C++

Looking back at the description of static in C language, we know:

  1. When static modifies local variables, the life cycle of local variables is extended.
  2. When . static modifies global variables, the external link attribute is changed into an internal link attribute, so that the scope of the global variable can only be executed in the source file.
  3. When . static modifies a function, the external link attribute is changed into an internal link attribute, so that the scope of the function can only be executed in the source file.

 concept

Class members declared as static are called static members of the class. Member variables modified with static are called static member variables; member functions modified with static are called static member functions. Static member variables must be initialized outside the class.

After a member in a class is modified with static, the member does not belong to a certain object, but to the entire class (because the static member exists in a static area).

As we have learned before, the member functions in a class do not exist together with the member variables in the object created by the class, but in the public code area. That is, when each object is created, there is no need to open up separate space for member functions, but only for non-static member variables.

static modified member variables

private:
	static int n;//属于整个类里的静态成员,不属于某个对象中
	static int m;//声明

The static modified member variables here cannot be given an initial value, because we know that if a value is given here, when the object is created, the initialization list will be used for initialization. If the initialization list does not give the corresponding member variable an initial value, and the member variable here If the default value is given when declaring, the program will naturally come here when it is executed and initialize with the default value. But if you use static to modify the static member variables, isn't the purpose so that the static variables will not be affected by the creation of the class? So this leads to: static member variables must be defined outside the class, and the static keyword is not added when defining. It is just a declaration in the class, and there is no need to add static modification when defining it, just indicate the class domain.

{
private:
	static int n;//属于整个类里的静态成员,不属于某个对象中
	static int m;//声明
};
int A::m = 0;//定义
int A::n = 0;

Private modifies the scope of the variable, indicating that it can only be used in this class. Static modifies the life cycle of the variable. It is loaded together when the class is loaded (generated before the object is generated), while other member variables are loaded when the object is instantiated, earlier than other member variables.


Member access

So how to access static member variables? ? ?

We know that general member variables are encapsulated as private, so objects outside the members cannot access private members, so we can first think of "unlocking": class static members can use class name::static members or objects. static members to access

Our general members can be accessed through object.member, and static members will have one more access method: class name::static member, because we already know that static members are not private members of an object, but It belongs to the entire class, so we can access it directly through the class name.

So is there any other way? ? ?

Another way is to create a member function in the class and access the members in the class by accessing the member function.

class A
{
public:
	A()
	{
		++m;
		++n;
	}
	void Print()
	{
		cout << m << " " << n << endl;
	}

private:
	static int n;//属于整个类里的静态成员,不属于某个对象中
	static int m;//声明
};
int A::m = 0;//定义
int A::n = 0;

int main()
{
	A a2;
	a2.Print();

}

static modified member function

Static can also modify functions. Just like the above, there is another way to access members. You can access them directly through classes without creating an object:

class A
{
public:
	A()
	{
		++m;
		++n;
	}
	static void Print()//静态成员函数(没有this指针)所以不能访问非静态成员
	{
		cout << m << " " << n << endl;
	}

private:
	static int n;//属于整个类里的静态成员,不属于某个对象中
	static int m;//声明
};
int A::m = 0;//定义
int A::n = 0;

int main()
{
	A::Print();

}

In the same way, we can understand that the member functions modified by static also belong to the entire class, so the member functions can be accessed directly through the class name. So at the same time, it leads to: static member functions have no hidden this pointer and cannot access any non-static members. The only thing a static function can access is a static variable or other static function

This is because when you call a static member function, you actually call it through the class name (even if you call it with an object), so when you pass parameters, you will not implicitly pass the object pointer, so there is no this pointer variable. , so non-static members of the class cannot be accessed within the function.

The last point is: static members are also members of the class and are restricted by public, protected, and private access qualifiers.
 

12f9789a1f6244ac97e836a9f7b49365.gif

Guess you like

Origin blog.csdn.net/C_Rio/article/details/132004680