C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++ static

C++中static修饰符的详解及其作用介绍

作者:我是小白呀

这篇文章主要介绍了C++中static修饰符的详解及其作用介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

概述

static (静态) 修饰符是用来控制变量的存储方式和可见性的. 静态局部变量存储在静态区域:

在这里插入图片描述

static 的性质:

静态数据成员

在我们定义全局变量的时候, 我们会发现一个问题:
我们可以在程序各处自由的修改全局变量的值 (不安全).

静态数据成员的特点:

  1. 静态数据成员被所有对象共享, 在所有对象之外单独开辟空间存储
  2. 静态数据成员所占空间并不随某个对象的撤销而释放
  3. 静态数据成员可以在同类的多个对象之间实现数据共享

在这里插入图片描述

引用静态数据成员

Student 类:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    static int count;  // 定义静态数据成员
    int num;
    string name;
    char gender;
public:
    Student();
    Student(int num, string name, char gender);
    ~Student();
    int getCount() {return count;}
    void display();
};

#endif //PROJECT1_STUDENT_H

Student.cpp:

#include "Student.h"
#include <iostream>
using namespace std;

// 类外初始化静态数据成员
int Student::count = 0;

// 无参构造
Student::Student() : num(-1), name("None"), gender('N') {}

Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
    count ++;
}

void Student::display() {
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "gender: " << gender << endl;
    cout << "===============" << endl;
}

Student::~Student() {
    count --;
}

main:

#include "Student.h"
#include <iostream>
using namespace std;

int main() {

    Student student1(1, "Little white", 'f');
    cout << student1.getCount() << endl;

    Student *pt = new Student(1, "Little white", 'f');
    cout << pt -> getCount() << endl;
    cout << student1.getCount() << endl;

    // 释放
    delete pt;
    cout << student1.getCount() << endl;

    return 0;
}

输出结果:

1
2
2
1

静态数据成员是 “大家” 的:

用类名访问数据成员

Student 类:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    int num;
    string name;
    char gender;
public:
    static int count;  // 定义静态数据成员
    Student();
    Student(int num, string name, char gender);
    ~Student();
    int getCount() {return count;}
    void display();
};

#endif //PROJECT1_STUDENT_H

Student.cpp:

#include "Student.h"
#include <iostream>
using namespace std;

// 类外初始化静态数据成员
int Student::count = 0;

// 无参构造
Student::Student() : num(-1), name("None"), gender('N') {}

Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
    count ++;
}

void Student::display() {
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "gender: " << gender << endl;
    cout << "===============" << endl;
}

Student::~Student() {
    count --;
}

main:

int main() {
    
    cout << Student::count << endl;
    Student *pt = new Student(1, "Little white", 'f');
    cout << pt -> getCount() << endl;

    // 释放
    delete pt;
    cout << Student::count << endl;

    return 0;
}

输出结果:

0
1
0

静态数据成员既可以通过对象名引用, 也可以通过类名来引用. 在作用域内, 通过类名和运算符 “::” 引用静态数据成员时, 不用考虑该类知否有对象存在.

静态成员函数

成员函数也可以定义为静态的, 我们只需要在类声明函数的前面加上 static 关键字. 如:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    int num;
    string name;
    char gender;
public:
    static int count;  // 定义静态数据成员
    Student();
    Student(int num, string name, char gender);
    ~Student();
    static int getCount() {return count;}  // 定义静态成员函数
    void display();
};

#endif //PROJECT1_STUDENT_H

静态成员函数的作用就是为了能处理静态数据成员, 即不需要 this 指针访问的成员.

重点:

综合案例

Student 类:

#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H

#include <string>
using namespace std;

class Student {
private:
    int num;
    string name;
    char gender;
    int score;
public:
    static int count;  // 定义静态数据成员
    static int sum;  // 定义静态数据成员
    Student();
    Student(int num, string name, char gender, int score);
    ~Student();
    static double average() {return (sum / count);}
    static int getCount() {return count;}
    void display();
};

#endif //PROJECT1_STUDENT_H

Student.cpp:

#include "Student.h"
#include <iostream>
using namespace std;

// 类外初始化静态数据成员
int Student::count = 0;
int Student::sum = 0;

// 无参构造
Student::Student() : num(-1), name("None"), gender('N'), score(-1) {}

Student::Student(int n, string p, char g, int s) : num(n), name(p), gender(g), score(s) {
    count ++;
    sum += s;
}

void Student::display() {
    cout << "num: " << num << endl;
    cout << "name: " << name << endl;
    cout << "gender: " << gender << endl;
    cout << "===============" << endl;
}

Student::~Student() {
    count --;
}

main:

#include "Student.h"
#include <iostream>
using namespace std;

int main() {

    // 创建student数组
    Student student_array[3] = {
            Student(1, "Little white", 'f', 68),
            Student(2, "Small white", 'f', 78),
            Student(3, "Big white", 'f', 88)
    };

    // 调试输出平均分
    cout << "三个学生的平均分是: " << Student::average() << endl;

    return 0;
}

输出结果:

三个学生的平均分是: 78

到此这篇关于C++中static修饰符的详解及其作用介绍的文章就介绍到这了,更多相关C++ static内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文