C++类成员函数中static变量小测试
来源:互联网 发布:福瑞博德软件开发 编辑:程序博客网 时间:2024/06/05 14:16
#include <iostream>using namespace std;class Test{public: int getStaticVariable() // member function belong to class not instances { static int i = 0; // belong to function, so static to class i++; return i; } private: int j;};int main(){ Test t1, t2, t3; cout << t1.getStaticVariable() << endl; // 1 cout << t2.getStaticVariable() << endl; // 2 cout << t1.getStaticVariable() << endl; // 3 cout << t3.getStaticVariable() << endl; // 4 return 0;}
使用Code::Blocks 12.11,运行结果:
1
2
3
4
t1, t2, t3共享getStaticVariable中i变量,即类拥有成员函数,成员函数中static变量也属于类。
0 0
- C++类成员函数中static变量小测试
- c++l类中static成员函数和成员数据变量使用
- c++l类中static成员函数和成员数据变量使用
- static 静态成员变量 静态成员函数 类中使用
- C/C++---static函数,static成员函数,static变量,static成员变量 再来理一理
- 【C++】C++ static成员变量与static成员函数
- static全局变量/全局变量,static函数/普通函数,函数中static变量/函数中的变量,类中的static成员变量/普通类成员变量
- static c ,C++类static 成员变量
- static c ,C++类static 成员变量
- static C,C++类static 成员变量
- C++中static成员函数访问非static成员变量
- C++中static成员函数访问非static成员变量
- [C++]static成员变量
- static成员函数成员变量
- 类的static成员变量,static成员函数
- c++中static成员函数、成员变量作用
- Static成员变量,static成员函数解析
- 【c++】内存偏移、成员函数和成员变量、static成员函数、变量
- Timus 1225. Flags Fibonacci的应用
- C语言程序员必读的五本书------转自嵌入式linux中文站
- Linux Shell脚本入门学习
- 深入学习Make命令和Makefile(上)(3)
- MFC 单文档窗口最大化显示
- C++类成员函数中static变量小测试
- 更改git clone时默认检出的分支
- 自定义security过滤器顺序
- CentOS 6.5搭建本地yum源的两种方式链接
- trie树 和 树的存储--左儿子右兄弟 --- uva 11732
- android 单元测试
- Oracle 分析函数及开窗函数 解析
- 深入学习Make命令和Makefile(上)(4)
- 在线程中执行带参数的委托