<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # namespace作用域 在c++中,名稱(name)可以是符號常量、變量、函數、結構、枚舉、類和對象等等。工程越大,名稱互相沖突性的可能性越大。另外使用多個廠商的類庫時,也可能導致名稱沖突。為了避免,在大規模程序的設計中,以及在程序員使用各種各樣的C++庫時,這些標識符的命名發生沖突,標準C++引入關鍵字namespace(命名空間/名字空間/名稱空間),可以更好地控制標識符的作用域。 命名空間下可以放函數,變量,結構體,類 **注意** 命名空間必須定義在全局作用域下 命名空間可以嵌套命名空間 ## 頭文件作用域 頭文件game1.h ~~~ #include<iostream> using namespace std; namespace g1 { void goAtk(); } ~~~ 實現game1.c ~~~ #include "game1.h" //實現的時候要加上作用域 void g1::goAtk() { cout << "測試" << endl; } ~~~ 調用 ~~~ #define _CRT_SECURE_NO_WARNINGS #include <iostream> //標準輸入輸出流 in輸入 out輸出 using namespace std; //使用命名空間 std打開一個叫std的房間 #include "game1.h" #include "game2.h" int main() { g1::goAtk(); getchar(); return 0; } ~~~ ## 變量作用域 ~~~ namespace A { int a = 20; } namespace B { int a = 10; } int main() { cout << "A: " << A::a << endl; cout << "B: " << B::a << endl; getchar(); return 0; } ~~~ ## 命名空間嵌套 ~~~ namespace A { int a = 20; namespace B { int a = 10; } } int main() { cout << "A:B: " << A::B::a << endl; getchar(); return 0; } ~~~ ## 命名空間是開放的 ~~~ namespace A { int a = 20; } namespace A { void func() { cout << "hello A" << endl; } } int main() { A::func(); cout << A::a << endl; getchar(); return 0; } ~~~ ## 無命名空間 意味著命名空間中的標識符只能在本文件內訪問,相當于給這個標識符加上了static,使得其可以作為內部連接 ~~~ namespace { int a = 10; void func() { cout << "hello world" << endl; } } int main() { cout << "a: " << a << endl; func(); getchar(); return 0; } ~~~ ## 別名 ~~~ namespace h1 { int a = 10; void func() { cout << "hello world" << endl; } } int main() { namespace h2 = h1; cout << h2::a << endl; getchar(); return 0; } ~~~ # using ## 聲明 ~~~ namespace A { int a = 11; } void test() { int a = 22; //using聲明 要注意避免二義性問題 //寫了using聲明后,下面這個代碼說明以后看到a都用A這個包 //但是編譯器有就近原則,產生二義性,下面代碼報錯 using A::a; cout << a << endl; } ~~~ ## 編譯指令 當引入一個全局的using編譯指令時,就為該文件打開了該命名空間,它不會影響任何其他的文件,所以可以在每一個實現文件中調整對命名空間的控制。比如,如果發現某一個實現文件中有太多的using指令而產生的命名沖突,就要對該文件做個簡單的改變,通過明確的限定或者using聲明來消除名字沖突,這樣不需要修改其他的實現文件。 ~~~ namespace A { int a = 11; } void test() { int a = 22; //using編譯指令,打開A命名空間,但是編譯器還是就近原則 using namespace A; cout << a << endl; } ~~~ 輸入cin ~~~ #include <iostream> #include <string> using namespace std; void test01() { //定義字符串變量 string str; //定義 int 變量 int age; //從控制臺獲取用戶輸入 cin >> str >> age; //將數據輸出到控制臺 cout << "str: " << str << " ---age: " << age << endl; } int main() { test01(); getchar(); return EXIT_SUCCESS; } ~~~
                  <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>

                              哎呀哎呀视频在线观看