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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 只有一個成員變量的類 有一些不太注意的缺省轉換發生,如從字符串常量到'char*'的轉換 <details> <summary>main.cpp</summary> ``` #include <iostream> #include <cstdlib> using namespace std; class classA { int x; public: classA(int x) { this->x = x; } classA(char *x) { this->x = atoi(x); } int getX() { return x; } }; int main ( ) { classA ca(5); //正常調用構造函數 cout << "x = " << ca.getX() << endl; ca = 100; //缺省調用classA(100) cout << "x = " << ca.getX() << endl; ca = "255"; //缺省調用classA("255") cout << "x = " << ca.getX() << endl; // 會發出警告: // main.cpp:17:8: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] // ca = "255"; // ^ // x = 5 // x = 100 // x = 255 return 0; } ``` </details> <br/> ## 用explicit禁止默認轉換 出現編譯報錯 <details> <summary>main.cpp</summary> ``` #include <iostream> #include <cstdlib> using namespace std; class classA { int x; public: explicit classA(int x) { this->x = x; } explicit classA(char *x) { this->x = atoi(x); } int getX() { return x; } }; int main ( ) { classA ca(5); //正常調用構造函數 cout << "x = " << ca.getX() << endl; ca = 100; //編譯出錯 cout << "x = " << ca.getX() << endl; ca = "255"; //編譯出錯 cout << "x = " << ca.getX() << endl; return 0; } ``` </details> <br/> ## 類的合成 當一個類的成員是另一個類時,稱之為“合成(composite)” <details> <summary>main.cpp</summary> ``` #include <iostream> using namespace std; class classB { int x; public: classB(int x) { this->x = x; } int getB() { return x; } }; class classA { classB xb; public: //classA(classB b) { xb = b; } //編譯出錯 classA(classB b) : xb(b) {} //只能用這種方法 int getX() { return xb.getB(); } }; int main() { classB cb(5); //先定義一個classB的實例 classA ca(cb); //然后用這個實例創建classA的實例 cout << "x = " << ca.getX() << endl; return 0; } ``` </details> <br/>
                  <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>

                              哎呀哎呀视频在线观看