静态对象的声明及初始化不是赋值

声明(并缺省初始化)后再赋值

#include <iostream>
#include <string> void fun(std::string input)
{
static int x; // 声明(及缺省初始化),因为声明为静态对象,所以只执行一次
x = 3; // 赋值, 第二次fun()被调用依然会被执行
std::cout << input << ": " << x << std::endl;
x = 4;
} int main()
{
fun("first");
fun("second");
}
>>>
first: 3
second: 3

声明并初始化后不赋值

#include <iostream>
#include <string> void fun(std::string input)
{
static int x = 3; // 声明并初始化为3,因为声明为静态对象,所以只执行一次
std::cout << input << ": " << x << std::endl;
x = 4;
} int main()
{
fun("first");
fun("second");
}
>>>
first: 3
second: 4

如果不声明为静态对象:

#include <iostream>
#include <string> void fun(std::string input)
{
int x = 3;
std::cout << input << ": " << x << std::endl;
x = 4;
} int main()
{
fun("first");
fun("second");
}
>>>
first: 3
second: 3

最新文章

  1. Ring3下的DLL注入(NtCreateThreadEx + LdrLoadDll方式实现,可以注入系统进程)
  2. 彻底解决Eclipse自动补全变量名及变量名后面追加类型名
  3. Emmet 生成 HTML 的语法
  4. PL/SQL Developer 连接新数据库
  5. Tea---hdu5881(规律)
  6. Network Saboteur(Rand版)
  7. wget多进程抓取的实现
  8. Vmware虚拟机网络模式及虚拟机与物理机通信方法
  9. Delphi中拖动无边框窗口的5种方法
  10. 通过php动态传数据到highcharts
  11. PHP原生DOM对象操作XML&#39;代码&#39;
  12. How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app
  13. 记一次IDEA编译器调优
  14. LeetCode(30):与所有单词相关联的字串
  15. win10常见问题处理办法
  16. 解决编译错误:cc: Internal error: Killed (program cc1)
  17. 判断exe是64位还是32位
  18. River Crossing---河南省第六届大学生程序设计竞赛
  19. HDU 6073 Matching In Multiplication(拓扑排序+思维)
  20. java的灵魂——反射

热门文章

  1. LeetCode-156 上下翻转二叉树
  2. fastai fit_one_cycle AttributeError: &#39;function&#39; object has no attribute &#39;parameters&#39;
  3. python cls方法_关于类方法中的cls
  4. golang json字符串合并操作
  5. 【python操作Excel的常见方法汇总】 xlrd pandas xlwings
  6. C#得到和JAVA通过Base64.encodeBase64String(DigestUtils.md5(&quot;&quot;))加密后的字符串
  7. VMware Workstation Pro 16、docker和Mysql相关
  8. VOLO论文笔记
  9. CodeGym自学笔记02——打印命令
  10. sql server 最近执行语句统计查询分析