c++ - 函数或代码中间的代码块
问题描述
像下面的例子一样,在函数或程序中间有代码块是什么意思?
#include <iostream>
#include <string>
using namespace std;
int main(){
int x = 32;
{ // random code block starts here
if (34 > x){
cout <<"x greater"<<endl;
}else cout << "no\n";
}// ends here
return 0;
}
解决方案
据我所知,这会创建一个新的scope
,因此在其中声明的任何对象或变量{}
都只能在那里使用。这对于创建实例特别有用,objects
因为destructor
对象超出范围时将被调用。
但是,在这种情况下,不需要 ,{}
因为没有声明变量或创建对象。
推荐阅读
- php - 安全和不推荐使用的过滤器
- javascript - 赛普拉斯抛出未定义的授权承载,并且未传递令牌
- c# - 创建无分区集合时,预置吞吐量集合应该有分区键错误
- php - 为什么我在资源中更改了许多内容后,在 WampServer 上运行的 PHP 项目中看不到任何更改?
- c# - WCF TLS 向后兼容性
- mysql - 在 Windows 7 上安装 MySQL 5.5 时出错
- javascript - 当我更新页面时,如何让我的所有页面自动更新?
- sql - Teradata 递归 SQL 查找给定子表的父级的父级
- reactjs - 反应表单:输入数据不会显示
- php - Google Analytics API 数据检索中的 401 Login Required 错误