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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C++ 拷貝構造函數 **拷貝構造函數**是一種特殊的構造函數,它在創建對象時,是使用同一類中之前創建的對象來初始化新創建的對象。拷貝構造函數通常用于: * 通過使用另一個同類型的對象來初始化新創建的對象。 * 復制對象把它作為參數傳遞給函數。 * 復制對象,并從函數返回這個對象。 如果在類中沒有定義拷貝構造函數,編譯器會自行定義一個。如果類帶有指針變量,并有動態內存分配,則它必須有一個拷貝構造函數。拷貝構造函數的最常見形式如下: ``` classname (const classname &obj) { // 構造函數的主體 } ``` 在這里,**obj** 是一個對象引用,該對象是用于初始化另一個對象的。 ``` #include <iostream> using namespace std; class Line { public: int getLength( void ); Line( int len ); // 簡單的構造函數 Line( const Line &obj); // 拷貝構造函數 ~Line(); // 析構函數 private: int *ptr; }; // 成員函數定義,包括構造函數 Line::Line(int len) { cout << "Normal constructor allocating ptr" << endl; // 為指針分配內存 ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Copy constructor allocating ptr." << endl; ptr = new int; *ptr = *obj.ptr; // copy the value } Line::~Line(void) { cout << "Freeing memory!" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "Length of line : " << obj.getLength() <<endl; } // 程序的主函數 int main( ) { Line line(10); display(line); return 0; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Normal constructor allocating ptr Copy constructor allocating ptr. Length of line : 10 Freeing memory! Freeing memory! ``` 下面的實例對上面的實例稍作修改,通過使用已有的同類型的對象來初始化新創建的對象: ``` #include <iostream> using namespace std; class Line { public: int getLength( void ); Line( int len ); // 簡單的構造函數 Line( const Line &obj); // 拷貝構造函數 ~Line(); // 析構函數 private: int *ptr; }; // 成員函數定義,包括構造函數 Line::Line(int len) { cout << "Normal constructor allocating ptr" << endl; // 為指針分配內存 ptr = new int; *ptr = len; } Line::Line(const Line &obj) { cout << "Copy constructor allocating ptr." << endl; ptr = new int; *ptr = *obj.ptr; // copy the value } Line::~Line(void) { cout << "Freeing memory!" << endl; delete ptr; } int Line::getLength( void ) { return *ptr; } void display(Line obj) { cout << "Length of line : " << obj.getLength() <<endl; } // 程序的主函數 int main( ) { Line line1(10); Line line2 = line1; // 這里也調用了拷貝構造函數 display(line1); display(line2); return 0; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Normal constructor allocating ptr Copy constructor allocating ptr. Copy constructor allocating ptr. Length of line : 10 Freeing memory! Copy constructor allocating ptr. Length of line : 10 Freeing memory! Freeing memory! Freeing memory! ```
                  <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>

                              哎呀哎呀视频在线观看