c++初始化和赋值的区别
2024-10-21 15:36:36
静态对象的声明及初始化不是赋值
声明(并缺省初始化)后再赋值
#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
最新文章
- Ring3下的DLL注入(NtCreateThreadEx + LdrLoadDll方式实现,可以注入系统进程)
- 彻底解决Eclipse自动补全变量名及变量名后面追加类型名
- Emmet 生成 HTML 的语法
- PL/SQL Developer 连接新数据库
- Tea---hdu5881(规律)
- Network Saboteur(Rand版)
- wget多进程抓取的实现
- Vmware虚拟机网络模式及虚拟机与物理机通信方法
- Delphi中拖动无边框窗口的5种方法
- 通过php动态传数据到highcharts
- PHP原生DOM对象操作XML&#39;代码&#39;
- How to Use Kdiff3 as a 3-way Merge Tool With Mercurial, Git, and Tower.app
- 记一次IDEA编译器调优
- LeetCode(30):与所有单词相关联的字串
- win10常见问题处理办法
- 解决编译错误:cc: Internal error: Killed (program cc1)
- 判断exe是64位还是32位
- River Crossing---河南省第六届大学生程序设计竞赛
- HDU 6073 Matching In Multiplication(拓扑排序+思维)
- java的灵魂——反射
热门文章
- LeetCode-156 上下翻转二叉树
- fastai fit_one_cycle AttributeError: &#39;function&#39; object has no attribute &#39;parameters&#39;
- python cls方法_关于类方法中的cls
- golang json字符串合并操作
- 【python操作Excel的常见方法汇总】 xlrd pandas xlwings
- C#得到和JAVA通过Base64.encodeBase64String(DigestUtils.md5(";";))加密后的字符串
- VMware Workstation Pro 16、docker和Mysql相关
- VOLO论文笔记
- CodeGym自学笔记02——打印命令
- sql server 最近执行语句统计查询分析