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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C++ 中的析構函數 > 原文: [https://beginnersbook.com/2017/08/cpp-destructors/](https://beginnersbook.com/2017/08/cpp-destructors/) 析構函數是一個特殊的成員函數,與構造函數相反,與用于初始化對象的[構造函數](https://beginnersbook.com/2017/08/cpp-constructors/)不同,析構函數銷毀(或刪除)對象。 **析構函數語法:** ```cpp ~class_name() { //Some code } ``` 與構造函數類似,析構函數名稱應與類名完全匹配。析構函數聲明應始終以波形符(`~`)符號開頭,如上面的語法所示。 ## 什么時候析構函數被調用? 在以下情況下,析構函數**自動調用**: 1)程序完成執行。 2)當包含[局部變量](https://beginnersbook.com/2017/08/cpp-variables/)的作用域(`{}`括號)結束時。 3)當你調用`delete`運算符時。 ## 析構函數示例 ```cpp #include <iostream> using namespace std; class HelloWorld{ public: //Constructor HelloWorld(){ cout<<"Constructor is called"<<endl; } //Destructor ~HelloWorld(){ cout<<"Destructor is called"<<endl; } //Member function void display(){ cout<<"Hello World!"<<endl; } }; int main(){ //Object created HelloWorld obj; //Member function called obj.display(); return 0; } ``` **輸出:** ```cpp Constructor is called Hello World! Destructor is called ``` ## 析構函數規則 1)名稱應以波形符號(`~`)開頭,并且必須與類名匹配。 2)一個類中不能有多個析構函數。 3)與可以有參數的構造函數不同,析構函數不允許任何參數。 4)他們沒有任何返回類型,就像構造函數一樣。 5)當你沒有在類中指定任何析構函數時,編譯器會生成一個默認的析構函數并將其插入到代碼中。
                  <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>

                              哎呀哎呀视频在线观看