網站首頁 編程語言 正文
static int a = 10;//在靜態區分配空間,不在堆棧
在靜態區分配空間,不在堆棧分配空間。因此,只有等到所以函數全部執行完成后,才會釋放空間。
一.static修飾變量
void text() { static int a = 10; a++; cout <<"a=" << a << endl; } int main(int arg, char** args) { text(); text(); }
結果:
二.static修飾函數
此函數的作用域只在本文件,其他文件無法調用。
static void text() { int a = 10; a++; cout <<"a=" << a << endl; }
三.static在類中使用
1.static成員變量實現了同類對象間信息共享。
2.static成員類外存儲,球類大小,并不包含在內。
3.static成員是命名空間屬于類的全局變量,存儲在data區(靜態區)。
4.static成員只能在類外初始化。
5.可以通過類名訪問(無對象生成時亦可),也可以通過對象訪問。
1.創建與初始化
先看錯誤:對于靜態變量的構造函數的錯誤。
class AA { public: AA(int a, int b,int c,int d) {//錯誤的構造函數 m_a = a; m_b = b; m_c = c; m_d = d; } static int m_c; private: int m_a; int m_b; static int m_d; };
正確寫法:靜態成員變量初始化,一定在類外。
class AA { public: static int m_c; AA(int a, int b) { m_a = a; m_b = b; } ~AA() { cout << "m_a=" << m_a << " m_b=" << m_b << " m_c=" << m_c << " m_d=" << m_d << endl; } private: int m_a; int m_b; static int m_d; }; int AA::m_c = 0;//public下靜態變量初始化 int AA::m_d = 1;//private下靜態變量初始化 int main(int arg, char** args) { AA a1(1, 2); }
結果:
2.使用問題
例子1:
class AA { public: AA(int a, int b) { m_a = a; m_b = b; } static int m_c; int get_c() { m_c++; return m_c; } private: int m_a; int m_b; }; int AA::m_c = 0; int main(int arg, char** args) { AA a1(1, 2); AA a2(10, 20); cout <<"a1(1, 2)下的c:" << a1.get_c() << endl; cout << "a2(10, 20)下的c:" << a2.get_c() << endl; }
結果:
原因:因為static分配變量在靜態區,因此類AA下所有變量(a1、a2)共用一個m_c。
因此,類中public下的static變量可以使用下面。訪問。但是private不可用。
cout<<AA::m_c << endl;
3.在public、private下static變量使用
在public下:可以通過AA::m_c = 100直接訪問和修改static變量值。
class AA { public: AA(int a, int b) { m_a = a; m_b = b; } static int m_c; private: int m_a; int m_b; }; int AA::m_c = 0; int main(int arg, char** args) { AA::m_c = 100; cout << AA::m_c << endl; }
在private下:必須提供函數,才能訪問和修改static變量值。
class AA { public: AA(int a, int b) { m_a = a; m_b = b; } static void set_md(int d) { m_d = d; } static int get_md() { return m_d; } private: int m_a; int m_b; static int m_d; }; int AA::m_d = 1; int main(int arg, char** args) { AA::set_md(100); cout << AA::get_md() << endl; }
結果:
四.class含有static變量所占空間
class A { private: int a; int b; static int c; }; int main() { cout << sizeof(A) << endl; }
結果:
結果:居然是8,不是12。因為static成員是命名空間屬于類的全局變量,存儲在data區(靜態區)。
五.練習題:求學生總人數、總分、平均分系統。
#include <iostream> using namespace std; class Student { private: int m_id; double m_score; static int count; static double total_score; public: Student(int id,double score) { m_id = id; m_score = score; count++; total_score += m_score; } static double get_total_score() { return total_score; } static double get_count() { return count; } }; int Student::count = 0; double Student::total_score = 0.0; int main(int arg, char** args) { Student S1(1, 50.1), S2(2, 10.1), S3(3, 0.1); //double mean_score = 0.0; cout << "學生人數:" << Student::get_count() << endl; cout << "學生總分:" << Student::get_total_score() << endl; cout << "學生平均分" << Student::get_total_score() / Student::get_count() << endl; }
總結
原文鏈接:https://blog.csdn.net/weixin_44190648/article/details/121966963
相關推薦
- 2022-08-17 C語言堆結構處理TopK問題詳解_C 語言
- 2022-09-05 內置指令、自定義指令(詳細)、全局指令與局部指令
- 2023-02-23 GoLang的sync.WaitGroup與sync.Once簡單使用講解_Golang
- 2022-10-29 .Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsS
- 2022-04-20 .NET?6新特性試用Timer類之PeriodicTimer?_ASP.NET
- 2023-04-29 React之關于Promise的用法_React
- 2022-06-29 Oracle中的常用函數詳解_oracle
- 2023-07-17 uniapp H5頁面內獲取手機號撥打電話
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支