<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一.命名空間概述 1. **全局命名空間污染** 程序開發中經常需要引用庫,若這些庫中含有與程序中定義的全局對象同名,或不同庫之間有同名的全局實體,則編譯時會出現命名沖突。 2. **作用** 建立一些互相分隔的作用域,把一些全局實體分隔開來,以免產生名字沖突。 3. **每個命名空間都是一個作用域** 位于命名空間之外的代碼訪問命名空間中的成員必須指明所用的命名空間的名字。 ## 二.命名空間定義 1. 定義一個命名空間 ```c++ namespace NAME {//名字是獨一無二的 //可以放入任何聲明 //code here }//后面沒有分號 ``` >命名空間的定義可以非連續,甚至跨越不同的程序文本文件,這樣可以頭文件定義接口,源代碼文件定義實現。 >命名空間支持嵌套。 ```c++ namespace name1 {//名字是獨一無二的 namespace name2 { //code here } //code here }//后面沒有分號 ``` 2. 命名空間成員的定義 + 在命名空間內,使用命名空間中定義的成員的名字無需前綴。 ```c++ namespace myns { class Student { public: int id; int score; }; Student max(Student stu1,Student stu2) { return stu1.score > stu2.score ? stu1 : stu2; } } ``` + 在命名空間外部定義成員之前需要在命名空間中聲明過。成員的名字必須被外圍命名空間名限定修飾。 + 若返回值是命名空間內定義的成員,則需要指明命名空間的名字。這是因為返回類型不在函數定義的域內。 ```c++ namespace myns { class Student { public: int id; int score; }; Student max(Student stu1,Student stu2); } myns::Student myns::max(Student stu1,Student stu2) { return stu1.score > stu2.score ? stu1 : stu2; } ``` 3. 訪問命名空間成員 命名空間成員的聲明被隱藏在其命名空間中,如需訪問,需要使用域操作符`::`。 ```c++ std::cout<<20; ``` ## 三.using 1. 使用命名空間 ```c++ using namespace 命名空間名稱; ``` 2. 使用命名空間的成員 ```c++ using 命名空間名稱::成員名; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看