[C++] Static member function (static member function concept | static member function declaration | static member function access | static member function can only access static members)





1. Introduction to static member functions




1. Concept of static member functions


Static member function ownership: In a C++ class, a static member function is a special function that belongs to the class rather than to the class instance object;

Static member function calls do not depend on objects: even if no instance object of the class is created, static member functions defined in the class can be called through the class name::;


Role of static member functions: Static member functions are usually used to perform operations related to the class itself. The execution of this function does not involve information in the class instance object, and ordinary member variables and member functions cannot be accessed in the static member function;


2. Static member function declaration


Static member function declaration: Use the static keyword to modify the member function to convert an ordinary member function into a static member function;

For example: the following void fun() ordinary member function, add the static keyword before the function, static void fun() becomes a static member function;

class Student
{
    
    
public:
	static void fun()
	{
    
    
		cout << "静态成员函数被调用 : number = " << number << endl;
	}
};

3. Static member function access


Static member function access:

  • Access using class name and domain operators:
	// 通过 类名:: 调用 静态成员函数
	Student::fun();
  • Use object access:
	// 通过 对象. 调用 静态成员函数
	s.fun();

4. Static member functions can only access static members


Static member function content requirements: Static member functions can only be accessed

  • static member variables
  • Other static member functions

Static member functions cannot access non-static member variables or non-static member functions.

Ordinary member variables and member functions need to be accessed through the instance object of the class and need to rely on the object to exist.

Static member functions can be called without creating an instance object, so non-static members cannot be accessed in static member functions;


If you access a non-static member in a static member function, the following error will be reported:

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>Hello.cpp
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(22,3): error C2597: 对非静态成员“Student::m_age”的非法引用
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Insert image description here





2. Code example - static member function



The following code is based on the previous static member variable sample code;

Define a static member function in the Student class and use the static keyword to modify the function;

Note: Do not access non-static members in static functions, otherwise an error "Illegal reference to non-static member "Student::m_age"" will be reported;

	static void fun()
	{
    
    
		// 静态成员函数 中 访问非静态成员会报错
		// error C2597: 对非静态成员“Student::m_age”的非法引用
		//m_age = 10;
		cout << "静态成员函数被调用 : number = " << number << endl;
	}

When accessing a static member function of a class, you can use class name:: to call the static member function;

	// 通过 类名:: 调用 静态成员函数
	Student::fun();

You can also use objects to call static member functions;

	// 通过 对象. 调用 静态成员函数
	s.fun();

Code example:

#include "iostream"
using namespace std;

class Student
{
    
    
public:
	// 带参构造函数
	Student(int age, int height)
	{
    
    
		m_age = age;
		m_height = height;
		cout << "执行 Student 的构造函数" << endl;
	}

	~Student()
	{
    
    
		cout << "执行 Student 的析构函数" << endl;
	}

	static void fun()
	{
    
    
		// 静态成员函数 中 访问非静态成员会报错
		// error C2597: 对非静态成员“Student::m_age”的非法引用
		//m_age = 10;
		cout << "静态成员函数被调用 : number = " << number << endl;
	}

public:
	int m_age;		// 年龄
	int m_height;	// 身高

	// 在类内部定义静态成员
	static int number;
};

// 在类外部初始化静态成员变量  
int Student::number = 1;

int main() {
    
    


	// I. 静态成员变量


	// 使用 域操作符 访问 类静态成员变量
	// 类名::静态成员变量名
	cout << "Student::number = " << Student::number << endl;

	// 在函数中为 类 静态成员变量 赋值
	Student::number = 2;

	// 创建 Student 类型对象
	Student s(10, 150);

	// 使用 对象 访问 类静态成员变量
	// 对象名称.静态成员变量名
	cout << "s.number = " << s.number << endl;


	// II. 静态成员函数


	// 通过 类名:: 调用 静态成员函数
	Student::fun();

	// 通过 对象. 调用 静态成员函数
	s.fun();

    // 控制台暂停 , 按任意键继续向后执行
    system("pause");

    return 0;
}

Results of the :

Student::number = 1
执行 Student 的构造函数
s.number = 2
静态成员函数被调用 : number = 2
静态成员函数被调用 : number = 2
请按任意键继续. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/133187910