C++ static 修饰符
C++ static 修饰符
- 概述
- 静态数据成员
- 引用静态数据成员
- 用类名访问数据成员
- 静态成员函数
- 综合案例
概述
static (静态) 修饰符是用来控制变量的存储方式和可见性的. 静态局部变量存储在静态区域:
static 的性质:
- 局部特性: 作用范围仅限于本函数
- 静态特性:存储在静态区, 函数调用结束后不孝顺而保留原值. 在下一次调用时, 保留上一次调用结束时的值.
静态数据成员
在我们定义全局变量的时候, 我们会发现一个问题:
我们可以在程序各处自由的修改全局变量的值 (不安全).
静态数据成员的特点:
- 静态数据成员被所有对象共享, 在所有对象之外单独开辟空间存储
- 静态数据成员所占空间并不随某个对象的撤销而释放
- 静态数据成员可以在同类的多个对象之间实现数据共享
引用静态数据成员
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 修饰符相关推荐
- java static修饰方法_浅析Java中static修饰符
1.概述 static关键字的中文意思是静态的,该修饰符可以修饰字段.方法.内部类.使用该关键字修饰的内容,在面向对象中static修饰的内容是隶属于类,而不是直接隶属于对象的,所以static修饰的 ...
- c++ static修饰符浅析
c++ static修饰符浅析 下面一段是引用自effective c++ 中的一句话: 所谓的static对象,其寿命是从构造出来到程序结束为止(以下文章不再赘诉).因此stack和heap-bas ...
- java(5)---static修饰符
static修饰符 static修饰符的特点: 1):static修饰的成员(字段/方法),随着所在类的加载而加载. 当JVM把字节码加载进JVM的时候,static修饰的成员已经在内存中存在了. 2 ...
- java反编译工具gd gson,浅谈Android中static修饰符,及Gson转String实例
肯定会很奇怪,为什么要把Gson和Static放一起去讲,因为在开发中,json这个东西是很常用的,不管是格式,还是数据的传递,还是我们与后台之间的交互,都离不开JSON,而今天,是在帮一个同行用ts ...
- static修饰符和final修饰符
static修饰符,修饰方法我们就可以不建立对象之久通过方法名调用. static修饰符,修饰属性属性就成了公共数据,并且数据只有一个.就像一个粉笔,A对象用完了粉笔B对象在用这跟粉笔就是A用剩下的那 ...
- 10.0_[Java 封装]-抽象/封装/构造/重载/ static 修饰符
################################################## 目录 抽象编程 面向对象设计的过程 类和对象的关系 为什么使用面向对象 this 关键字 类的成员 ...
- 面向对象通识14(static修饰符)
面向对象通识14(static修饰符) static并不是字面意义上"静态"的意思,而是类的意思-- 有static的成员是类成员 没有static的成员是实例成员 //在C语言中 ...
- Java中static修饰符的作用
近日在做网络通信服务器和客户端的Java语言开发,碰到了获取客户端长连接Channel对象,利用唯一对象Client的获取方法getSingleClientInstance(),以为可以得到,但是显示 ...
- 什么是static修饰符?
一.什么是static修饰符 1.static是静态意思,可以修饰成员变量和成员方法 2.static修饰成员变量表示该成员变量在内存中只存储一份,可以被共享访问,修改. 二.成员变量,成员方法的分类 ...
最新文章
- 【java】Maven工程引入各种jar包的功能
- 莫队 ---- CF 135D. Jeff and Removing Periods (等差数列预处理 + 莫队)
- 第六章 ppp协议实验
- 还在封装各种 Util 工具类?这个神级框架帮你解决所有问题!
- JavaScript零散知识点总结
- 库存管理-历史库存和收发存系列-MB5B
- 零起点学算法82——数组中查找数
- mysql主从只同步部分库或表
- spring注解 @Scheduled(cron = 0 0 1 * * *)的使用来实现定时的执行任务
- 数不清的Gmail邀请发放中……
- 基于Spring Boot 的统一错误处理
- 移动端页面制作字号大小设定问题,设计稿文字字号规范,解决移动端大小屏适配问题
- error: Setup script exited with error: Unable to find vcvarsall.bat
- 解决C:/WINDOWS/system32/aliedit/a找不到文件的错误提示
- 携程酒店数据爬取(新)
- ubuntu mysql的my.cnf_Ubuntu下面MySQL的参数文件my.cnf浅析
- 最长上升子序列 CSU - 1047 ( LIS LCS )
- 咳咳,建模大神教你如何用ZBrush雕刻肌肉上课时间到了
- 用python写一个解一元二次方程的类
- 【p4】perforce命令笔记