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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

我是小白呀   2021-09-07 我要评论
想了解C++中static修饰符的详解及其作用介绍的相关内容吗,我是小白呀在本文为您仔细讲解C++ static的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们