我的意思是除了在函数,类,if,while,switch,try-catch需要时使用它.
在我看到这个问题之前,我不知道可以这样做.
在上面的链接中,Eli提到"他们使用它来折叠代码的逻辑部分,这些逻辑部分不属于通常折叠起来的函数,类,循环等."
除了提到的那些之外还有什么其他用途?
使用花括号来限制变量的范围并仅在需要时扩展范围(在"需要访问"的基础上工作)是一个好主意吗?或者它真的很傻?
如何使用范围,以便您可以在不同的范围内使用相同的变量名称,但在相同的更大范围内?或者,重用相同的变量(如果你想使用相同的变量名)是更好的做法,并节省解除分配和分配(我想一些编译器可以对此进行优化?)?或者更好地使用不同的变量名称?
是否有任何情况下将代码放在括号内以缩小其范围是我可能想要做的事情,或者这是你们会告诉我的那种情况之一,"如果你需要在你的代码中这样做,那么你的代码编写得很糟糕."
例如:
//***CODE****
{
  int foo=stuff;
  //use foo, and then I'm done using it forever
}
//****MORE CODE****
我有时会使用大括号来隔离代码块,以避免以后错误地使用变量.例如,当我SqlCommand在同一个方法中放入几个s时,我经常复制粘贴代码块,最后混合名称并执行两次命令.添加大括号有助于避免这种情况,因为SqlCommand在错误的位置使用错误将导致错误.这是一个例子:
Collection<string> existingCategories = new Collection<string>();
// Here a beginning of a block
{
    SqlCommand getCategories = new SqlCommand("select Title from Movie.Category where SourceId = @sourceId", sqlConnection, sqlTransaction);
    getCategories.Parameters.AddWithValue("@sourceId", sourceId);
    using (SqlDataReader categoriesReader = getCategories.ExecuteReader(System.Data.CommandBehavior.SingleResult))
    {
        while (categoriesReader.Read())
        {
            existingCategories.Add(categoriesReader["Title"].ToString());
        }
    }
}
if (!existingCategories.Contains(newCategory))
{
    SqlCommand addCategory = new SqlCommand("insert into Movie.Category (SourceId, Title) values (@sourceId, @title)", sqlConnection, sqlTransaction);
    // Now try to make a mistake and write/copy-paste getCategories instead of addCategory. It will not …从技术上讲,在 C++ 中,我们可以使用大括号来声明新的作用域。例如在这个函数中,它交换两个数字
void swap_int(int& first, int& second)
{
   int temp = first;
   first = second;
   second = temp;
}
我们还可以temp在它自己的块内声明:
void swap_int(int& first, int& second)
{
   // Do stuf...
   {
      int temp = first;
      first = second;
      second = temp;
   }
   // Do other stuff...
}
这样做显然有一个好处,就是temp不再需要的时候就直接删除。
但是,在我编写的代码中我从未使用过它。另外,在第三方库的代码中我几乎从未见过它。
为什么不公开使用?它是否会带来任何性能提升,或者只是意味着额外的打字工作?
我遇到了一个项目,在那里我发现了一些我无法理解的代码.我刚开始C++,所以对我来说这似乎是个大问题.我提供的项目很少,我无法理解.
class abc
{
     public:
       //some stuff
       abc();
};
abc::abc()
{
    int someflag = 0;
    //code
    if(someflag == 0)
    {
         do
         {
             //few strcpy operations
             {                 //(My Question)Without any condition braces started
                   //variable initialization
             }
         }while(condition);
    }
}
现在我的问题......
do-while循环的内部括号中发生了什么?do-while循环内初始化变量(我提到过)的范围是什么
 ?帮我理解这个.
虽然下面的例子编译得很好,除了错误的最后一行,我想知道范围内这个'范围'的来龙去脉?也是这个术语,如果有的话.
考虑这些括号:
void func()
{
    int i = 0;
    { // nice comment to describe this scope
        while( i < 10 )
            ++i;
    }
    { // nice comment to describe this scope
        int j= 0;
        while( j< 10 )
            ++j;
    }
    i = 0; // OK
    // j = 0; // error C2065
}
考虑一下:
error C2065: 'j' : undeclared identifier
编辑: 接受的答案来自位掩码,虽然我认为每个人都应该把它放在anio的答案中.特别是,引用:"也许你应该将你的功能分解为2个功能"
我正在尝试解决c ++中的一些问题,但我以前从未见过这个表达式.由于我不知道表达式是什么,我不能谷歌它.
int main() {
   int something=1;
   {
     assert(something);
   }
   // What is the purpose of these braces?
   // and what is it called?
   {
     assert(something != NULL);
   }
   return;
}
c++ ×6
scope ×4
c# ×1
c++11 ×1
coding-style ×1
curly-braces ×1
dos-donts ×1
java ×1
stylecop ×1