C++里的花括号{},块,作用域
生活随笔
收集整理的這篇文章主要介紹了
C++里的花括号{},块,作用域
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
{ } 里的內容是一個“塊”。
單獨的{ }在執行順序上沒有改變,仍然是順序執行,不同的是標識符的作用域限定 。
#include <iostream> #include <string> using namespace std;
class tcalss { ????string Name; public: ????tcalss(string name){Name=name;cout<<"build "<<Name<<endl;}; ????~tcalss(){cout<<"kill "<<Name<<endl;}; };
int main() {
????int Test=100; ????cout<<"out of {} Test ="<<Test<<endl;
????tcalss x("class out of {}"); ??? ?????{ ?????????tcalss y("class in {}"); ?????????float Test=3.14159; ?????????cout<<"in {} Test= "<<Test<<endl; ?????} ??? ????cout<<"Out Of {} Test="<<Test<<endl; }?
====================================================================================================== 花括號可以看作是作用域標識符,除了在寫函數時候用到,它還有一個作用就是表示變量的作用域: {int a=0;{int b=0;a=1; //正確,還在a的作用域中}b=1; //錯誤,因為不在b的作用域,b已經被銷毀了 } ========================================================================================================== 參考:http://www.360doc.com/content/14/0417/11/11948835_369707933.shtml 在C/C++中大括號指明了變量的作用域,在大括號內聲明的局部變量其作用域自變量聲明開始,到大括號之后終結。 void MyProcess(MyType input, MyType &output) { MyType filter = input;
? ?{ ? ? ? MyType temp; ? ? ? step1(filter,temp); ? ?}
? ?{ ? ? ? MyType temp; ? ? ? step2(filter,temp); ? ?}
? ?{ ? ? ? MyType temp; ? ? ? step3(filter,temp); ? ?} ? ?output = filter; }
以上程序實現了簡單的管道/過濾器結構: temp1 ? ?temp2 ? temp3 | ? ? ? | ? ? ?| input --> step1 --> step2 --> step3 --> output temp都是臨時變量,如果沒有大括號的約束,每個臨時變量都存在于函數作用域中,那么頻繁增減流程時出錯的概率大大增加。 放在大括號中,不僅程序閱讀起來很清楚,而且也不容易出錯
單獨的{ }在執行順序上沒有改變,仍然是順序執行,不同的是標識符的作用域限定 。
#include <iostream> #include <string> using namespace std;
class tcalss { ????string Name; public: ????tcalss(string name){Name=name;cout<<"build "<<Name<<endl;}; ????~tcalss(){cout<<"kill "<<Name<<endl;}; };
int main() {
????int Test=100; ????cout<<"out of {} Test ="<<Test<<endl;
????tcalss x("class out of {}"); ??? ?????{ ?????????tcalss y("class in {}"); ?????????float Test=3.14159; ?????????cout<<"in {} Test= "<<Test<<endl; ?????} ??? ????cout<<"Out Of {} Test="<<Test<<endl; }?
====================================================================================================== 花括號可以看作是作用域標識符,除了在寫函數時候用到,它還有一個作用就是表示變量的作用域: {int a=0;{int b=0;a=1; //正確,還在a的作用域中}b=1; //錯誤,因為不在b的作用域,b已經被銷毀了 } ========================================================================================================== 參考:http://www.360doc.com/content/14/0417/11/11948835_369707933.shtml 在C/C++中大括號指明了變量的作用域,在大括號內聲明的局部變量其作用域自變量聲明開始,到大括號之后終結。 void MyProcess(MyType input, MyType &output) { MyType filter = input;
? ?{ ? ? ? MyType temp; ? ? ? step1(filter,temp); ? ?}
? ?{ ? ? ? MyType temp; ? ? ? step2(filter,temp); ? ?}
? ?{ ? ? ? MyType temp; ? ? ? step3(filter,temp); ? ?} ? ?output = filter; }
以上程序實現了簡單的管道/過濾器結構: temp1 ? ?temp2 ? temp3 | ? ? ? | ? ? ?| input --> step1 --> step2 --> step3 --> output temp都是臨時變量,如果沒有大括號的約束,每個臨時變量都存在于函數作用域中,那么頻繁增減流程時出錯的概率大大增加。 放在大括號中,不僅程序閱讀起來很清楚,而且也不容易出錯
總結
以上是生活随笔為你收集整理的C++里的花括号{},块,作用域的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Selenium的Web UI自动化
- 下一篇: LFW database