• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 181  文章 - 15  trackbacks - 0
            <2009年1月>
            28293031123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            My Tech blog

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            在這一部分之前,書中介紹了基本類型的顯式初始化以及簡單的異常處理.
            基本類型的顯式初始化是比較簡單的.就是說你在定義一個整型變量的時候,有兩種不同的情況:
            int i1;         // undefined value
            int i2 = int(); // initialized with zero

            如果按照前一種,會作"值未定義;如果按照后一種,則自動被初始化為0.這樣也就確保了你的類在初始化的時候有一個確定的初始值.
            至于異常的處理等問題,書中會在后面有比較詳細的描述.這里可以看到比較有意思的一點,就是指定函數拋出的異常類型,這于Java很像:
            void f() throw(bad_alloc);
            下面轉入正題:命名空間.
            有了命名空間,它將會取代函數和類作用于全局,并作為它所統領的那些類和函數的唯一標識存在.這樣可以避免命名沖突情況的出現.正如書中所說:
            Unlike classes, namespaces are open for definitions and extensions in different modules. Thus
            you can use namespaces to define modules, libraries, or components even by using multiple
            files. A namespace defines logical modules instead of physical modules (in UML and other
            modeling notations, a module is also called a package).

            可以像這樣定義一個命名空間:
            namespace MyNameSpace
            {
                
            class MyClass
                {
                    
            private:
                    
            char * _classInfo;
                    
            public:
                    
            char* getClassInfo()
                    {
                        
            return _classInfo;
                    }
                    MyClass(
            const char* info)
                    {
                        _classInfo
            =new char[strlen(info)];
                        strcpy(_classInfo,info);
                    }
                    
            ~MyClass()
                    {
                        
            if(_classInfo)
                        {
                            std::cout
            <<"free classinfo";
                            delete[] _classInfo;
                        }
                    }
                };
                
            void printMyClassInfo(MyClass &instance)
                {
                    std::cout
            <<instance.getClassInfo();
                }
            }
            從上面可以看出,這個命名空間里面包括了一個類和一個函數.類中包含了char*類型的成員變量.函數printMyClassInfo 以一個MyClass類型的引用作為參數.為什么要用引用呢?熟悉c++的人應當很清楚,我是通過實驗才剛剛知道原因.這個原因我將會在后面說明.
            好現在來看一下調用過程,通常的調用過程是這樣的:
            int main()
            {
                MyNameSpace::MyClass instance(
            "MyClass!\n");
                MyNameSpace::printMyClassInfo(instance);
            }
            這沒有任何問題,但有意思的是,還可以這樣調用:
            int main()
            {
                MyNameSpace::MyClass instance(
            "MyClass!\n");
                printMyClassInfo(instance);
            }
            看來c++中在使用一個命名空間的類或者函數的時候,這個命名空間就被"自動"引入了.當尋找函數printMyClassInfo的時候會在當前的上下文中進行尋找的同時,還會到以前用到過的命名空間中去尋找.
            當然,通常情況下我們喜歡這樣做:
            using namespace MyNameSpace; 
            int main()
            {
                MyClass instance(
            "MyClass!\n");
                printMyClassInfo(instance);

            }
            但是并不是在任何情況下都鼓勵using namespace這種做法的.在書中將得比較清楚:
            Note that you should never use a using directive when the context is not clear (such as in header
            files, modules, or libraries). The directive might change the scope of identifiers of a namespace,
            so you might get different behavior than the one expected because you included or used your
            code in another module. In fact, using directives in header files is really bad design.

            上面這段話強調了當上下文并不明確的情況下(比如在一個頭文件,組件或者庫里面),不要使用using這種寫法,這個指令會改變命名空間標識符的作用域,這樣你就有可能引發和你預期不相同的行為,因為你會在另外一個組件中引用你的代碼或使用它.事實上,將using標識符寫在頭文件里面是一種相當不好的設計.
            在這里,我看了一下c++程序設計語言這本書,發現命名空間除了像上面這樣聲明以外,還可以像類一樣這樣來寫:
            在命名空間中這樣定義
            void printMyClassInfo(MyClass &);
            然后在外面寫函數的主體
            void MyNameSpace::printMyClassInfo(MyClass &instance)
            {
                std::cout
            <<instance.getClassInfo();
            }
            好了,寫了這么多,再來看看剛才留下來的那個問題.
            其實很簡單,一個函數如果傳遞的是值,那么就會在內存中產生一個一模一樣的"復本",而那個字符指針也會被復制一次.當傳送的值超過它的作用域的時候 ,就會被釋放掉,而被復制的"本體"在程序運行結束之后,又會被"釋放一次".這樣在運行的時候,它會提示你這樣的錯誤:
            *** glibc detected *** double free or corruption (fasttop): 0x0804a008 ***

            在我們的MyClassl類的析構中,我們有一個輸出,所以這里就輸出了兩次:
            free classinfofree classinfo




            posted on 2007-06-14 22:06 littlegai 閱讀(284) 評論(0)  編輯 收藏 引用 所屬分類: 我的讀書筆記
            久久综合久久综合九色| 日日狠狠久久偷偷色综合免费 | 久久午夜免费视频| 99久久做夜夜爱天天做精品| 99久久无色码中文字幕人妻| 东京热TOKYO综合久久精品| 99久久精品国产一区二区| 亚洲国产香蕉人人爽成AV片久久| 偷偷做久久久久网站| 久久精品免费观看| 亚洲欧美久久久久9999| 99麻豆久久久国产精品免费| 久久93精品国产91久久综合| 亚洲国产欧美国产综合久久 | 看久久久久久a级毛片| 久久精品国产72国产精福利| 亚洲精品蜜桃久久久久久| 国产成人精品综合久久久| 久久九九兔免费精品6| 国产精品青草久久久久福利99| 久久精品国产色蜜蜜麻豆| AAA级久久久精品无码区| 免费久久人人爽人人爽av| 久久99久久无码毛片一区二区| 性欧美大战久久久久久久久| 久久免费国产精品| 国产精品美女久久久久av爽| 大伊人青草狠狠久久| 久久一日本道色综合久久| 思思久久99热只有频精品66| 久久久久99精品成人片三人毛片| 国产91色综合久久免费| 久久ww精品w免费人成| 久久AV无码精品人妻糸列| 性做久久久久久免费观看| 精品久久久久久99人妻| 欧美精品一本久久男人的天堂| 久久久国产精品网站| 国产精品毛片久久久久久久| 国产一级持黄大片99久久| 久久精品九九亚洲精品天堂|