<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之旅 廣告
                我們在external目錄中建立一個C++應用程序lightpointer來說明輕量級指針的使用方法,它的目錄結構如下: ~~~ ~/Android ----external ----lightpointer ----lightpointer.cpp ----Android.mk ~~~ 應用程序lightpointer的實現很簡單,它只有一個源文件lightpointer.cpp和一個編譯腳本文件Android.mk。下面我們就分別介紹它們的內容。 **lightpointer.cpp** ~~~ #include <stdio.h> #include <utils/RefBase.h> using namespace android; class LightClass : public LightRefBase<LightClass> { public: LightClass() { printf("Construct LightClass Object."); } virtual ~LightClass() { printf("Destroy LightClass Object."); } }; int main(int argc, char** argv) { LightClass* pLightClass = new LightClass(); sp<LightClass> lpOut = pLightClass; printf("Light Ref Count: %d.\n", pLightClass->getStrongCount()); { sp<LightClass> lpInner = lpOut; printf("Light Ref Count: %d.\n", pLightClass->getStrongCount()); } printf("Light Ref Count: %d.\n", pLightClass->getStrongCount()); return 0; } ~~~ 這個文件創建了一個繼承了LightRefBase類的LightClass類,因此,LightClass類的對象能夠結合輕量級指針來使用。 在應用程序lightpointer的入口函數main中,首先創建了一個LightClass對象pLightClass,然后再創建一個輕量級指針lpOut來引用它。在輕量級指針lpOut的創建過程中,會調用sp類的構造函數來增加LightClass對象pLightClass的引用計數,即此時LightClass對象pLightClass的引用計數值為1,因此,第25行的printf語句打印出來的數字就應該等于1。在第27行到第31行代碼中,我們在一個內嵌的作用域中創建了另外一個輕量級指針lpInner來引用LightClass對象pLightClass,此時LightClass對象pLightClass的引用計數值會再增加1,因此,第30行的printf語句打印出來的數字就應該等于2。當應用程序lightpointer跳出了第27行到第31行的作用域之后,輕量級指針lpInner就被析構了,這時候LightClass對象pLightClass的引用計數值就會減少1,因此,第33行的printf語句打印出來的數字就應該等于1。當應用程序lightpointer執行完成之后,輕量級指針lpOut也會被析構,這時候LightClass對象pLightClass的引用計數值就會再次減少1,即等于0,于是LightClass對象pLightClass就會被釋放。 **Android.mk** ~~~ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_MODULE := lightpointer LOCAL_SRC_FILES := lightpointer.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ libutils include $(BUILD_EXECUTABLE) ~~~ 這是應用程序lightpointer的編譯腳本文件,它引用了libcutils和libutils兩個庫。 上述兩個文件準備好了之后,我們就可以對這個C++工程進行編譯和打包了。 ~~~ USER@MACHINE:~/Android$ mmm ./external/lightpointer/ USER@MACHINE:~/Android$ make snod ~~~ 編譯成功之后,就可以在out/target/product/gerneric/system/bin目錄下看到應用程序文件lightpointer了;打包成功之后,該應用程序就包含在out/target/product/gerneric目錄下的Android系統鏡像文件system.img中了。 最后,我們使用新得到的系統鏡像文件system.img來啟動Android模擬器。Android模擬器啟動起來之后,使用adb工具連接上它,并且進入到/system/bin目錄中,運行應用程序lightpointer來查看它的輸出,從而驗證輕量級指針的實現原理。 ~~~ USER@MACHINE: ~/Android$ emulator & USER@MACHINE:~/Android$ adb shell root@android:/ # cd system/bin/ root@android:/system/bin # ./lightpointer Construct LightClass Object. Light Ref Count: 1. Light Ref Count: 2. Light Ref Count: 1. Destory LightClass Object. ~~~ 如果能看到上面的輸出,就說明我們前面對輕量級指針的實現原理的分析是正確的。
                  <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>

                              哎呀哎呀视频在线观看