• <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>
            隨筆 - 96  文章 - 255  trackbacks - 0
            <2008年4月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            E-mail:zbln426@163.com QQ:85132383 長期尋找對戰(zhàn)略游戲感興趣的合作伙伴。

            常用鏈接

            留言簿(21)

            隨筆分類

            隨筆檔案

            SDL相關(guān)網(wǎng)站

            我的個(gè)人網(wǎng)頁

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 493480
            • 排名 - 39

            最新評論

            閱讀排行榜

            評論排行榜

                    假設(shè)我們有一個(gè)類A,另外一個(gè)類B中有個(gè)成員數(shù)據(jù)是A的對象,我們?nèi)绾蝸矶x這個(gè)成員數(shù)據(jù)呢?
            最直接的,我們可以就把它定義為A的對象:A a_obj;
            其次,我們可以把它定義為A對象的引用:A& a_obj; 或者const引用:const A& a_obj;
            再者,我們還可以把它定義為A對象的指針:A* pA_obj; 或者const對象的指針:const A* pA_obj;
                    當(dāng)我們直接使用A對象的時(shí)候,a_obj會在B對象建立的時(shí)候建立,在B銷毀的時(shí)候銷毀。因?yàn)檫@是一個(gè)新建立的對象,所以在成員初始化的時(shí)候是真正構(gòu)造了對象,即我們前面說到的使用了復(fù)制構(gòu)造函數(shù)。
                    如果使用的是A對象的引用或者指針,a_obj(或pA_obj)都沒有構(gòu)造新的對象。我們假設(shè)B的一個(gè)構(gòu)造函數(shù)是這樣的:B(const A& a_obj): a_obj(a_obj) {},那么毫無疑問,這個(gè)構(gòu)造函數(shù)是能正常工作的,也就是說,我們用現(xiàn)成的對象,或者說是在B對象生命周期內(nèi)都會一直存在的A對象去構(gòu)造B對象是沒有問題的。但是,如果一個(gè)構(gòu)造函數(shù)是這樣的:B(int a): a_obj(A(a)) {},這個(gè)函數(shù),一般在編譯的時(shí)候是不會報(bào)錯(cuò)或者發(fā)出警告的,但是我們分析一下,A(a)是臨時(shí)構(gòu)造了一個(gè)A對象,然后a_obj成為了這個(gè)臨時(shí)對象的引用。但是問題是,這個(gè)臨時(shí)對象在B對象構(gòu)造之后就馬上銷毀了,也就是說,a_obj引用了一個(gè)不存在的對象(換到指針說就是指向了空指針)。這是一個(gè)巨大的錯(cuò)誤,將在運(yùn)行時(shí)引發(fā)錯(cuò)誤。
                    所以,結(jié)論是:如果成員數(shù)據(jù)使用(新)對象,則必須定義這個(gè)對象所屬類的復(fù)制構(gòu)造函數(shù);如果使用的是對象引用或者指針,則一定只能用已經(jīng)存在并且會在B對象整個(gè)生命周期內(nèi)都存在的A對象來構(gòu)造這個(gè)B對象。
                    說得貌似很復(fù)雜,留個(gè)作為例子的3個(gè)類,大家可以多寫幾個(gè)演示程序試試。
            #ifndef A_HPP
            #define A_HPP

            #include 
            <iostream>
            using namespace std;

            char* get_point(int lenth);
            void free_point(char* temp);

            class A
            {
            private:
                
            //
            protected:
                
            char* temp;
                
            int lenth;
                A();
            public:
                A(
            const A& copy);
                
            ~A();
                
            void show() const;
            };

            class C: public A
            {
            public:
                C(
            int _lenth);
                
            ~C();
            };


            class B
            {
            private:
                A b;
            public:
                B(
            const A& a);
                B(
            int _lenth);
                
            ~B();
                
            void show() const;
            };

            #endif

            #include "a.hpp"

            A::A(): temp(
            0),lenth(0)
            {
                cout
            << "A Constructor!" << endl; 
            }


            A::A(
            const A& copy): lenth(copy.lenth)
            {
                temp 
            = get_point(lenth);
                cout 
            << "A Copy Constructor" << endl;
                cout 
            << "temp at: " << int(temp) << endl;
            }


            A::
            ~A()
            {
                cout 
            << "temp at: " << int(temp) << endl;
                free_point(temp);
                cout 
            << "Heap Deleted!\n";
                cout 
            << "A Destroyed!" << endl; 
            }

            void A::show() const
            {
                cout 
            << temp << endl;
            }


            //***************************************


            C::C(
            int _lenth): A()
            {
                lenth 
            = _lenth;
                temp 
            = get_point(lenth);
                cout
            << "C Constructor!" << endl; 
                cout 
            << "temp at: " << int(temp) << endl;
            }

            C::
            ~C()
            {
                cout 
            << "C Destroyed!" << endl; 
            }

            //***************************************

             
            B::B(
            const A& a): b(a)
            {
                cout
            << "B Constructor!" << endl; 
            }

            B::B(
            int _lenth): b(C(_lenth))
            {
                cout
            << "B Constructor!" << endl;
            }

            B::
            ~B()
            {
                cout 
            << "B Destroyed!" << endl; 
            }

            void B::show() const
            {
                b.show();
            }


            //************************************


            char* get_point(int lenth)
            {
                
            char* temp = new char[lenth+1];
                
            for ( int i = 0; i< lenth; i++ )
                    temp[i] 
            = '*';
                temp[lenth] 
            = '\0';
                cout 
            << "New buffer got!\n";
                
            return temp;
            }

            void free_point(char* temp)
            {
                delete []temp;
                cout 
            << "Buffer deleted!\n";
            }
            久久久久久久免费视频| 51久久夜色精品国产| 99久久国产亚洲综合精品| 久久久久青草线蕉综合超碰| 一本色道久久综合亚洲精品| 久久精品无码专区免费东京热| 国内精品久久九九国产精品| 久久久WWW免费人成精品| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久免费视频观看| 久久九九久精品国产| 久久99热狠狠色精品一区| 香蕉久久夜色精品国产尤物| 狠狠色噜噜狠狠狠狠狠色综合久久 | 精品国产乱码久久久久久1区2区| 女同久久| 久久国产精品免费一区| 国产美女亚洲精品久久久综合| 91精品婷婷国产综合久久| 18禁黄久久久AAA片| 精品久久人人爽天天玩人人妻 | 91精品国产综合久久精品| 久久精品一本到99热免费| 无码8090精品久久一区 | 久久综合九色综合网站| 国产综合精品久久亚洲| 久久精品视频网| 99精品久久精品| 久久超乳爆乳中文字幕| 久久人人爽人人爽人人AV东京热 | 久久久久久精品无码人妻| 日日狠狠久久偷偷色综合0| 久久精品无码一区二区三区免费 | 亚洲欧美国产日韩综合久久| 久久一本综合| 亚洲国产精品无码久久九九| 亚洲国产成人久久精品99| 一级女性全黄久久生活片免费| 四虎影视久久久免费| 亚洲欧美日韩中文久久| 久久婷婷五月综合97色|