• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 嵌套對象的copy-constructor

    發表于:2007-05-25來源:作者:點擊數: 標簽:當用嵌套對象
    當用一個已經初始化過了的自定義類類型對象去初始化另一個新構造的對象的時候,拷貝構造函數就會被自動調用,如果你沒有自定義拷貝構造函數的時候系統將會提供給一個默認的拷貝構造函數來完成這個過程。Copy構造函數有一個參數,這個參數是一個該類的對象。
    當用一個已經初始化過了的自定義類類型對象去初始化另一個新構造的對象的時候,拷貝構造函數就會被自動調用,如果你沒有自定義拷貝構造函數的時候系統將會提供給一個默認的拷貝構造函數來完成這個過程。Copy構造函數有一個參數,這個參數是一個該類的對象。例如CString的構造函數CString( const CString& stringSrc )就是一個Copy構造函數。

    代碼:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    ofstream out("autoclearcase/" target="_blank" >cc.out");
    class inside_withCC {
            static int inside_withCC_object_count;
    public:
        inside_withCC(){
        out << "inside_withCC constructions function is called" << endl;
        inside_withCC_object_count++;
        out << "inside_withCC(): inside_withCC_object_count = " << inside_withCC_object_count << endl;
        }
        inside_withCC(const inside_withCC&) {
            out << "inside_withCC copy-constructions function is called" << endl;
            inside_withCC_object_count++;
        out << "inside_withCC(const withCC&): inside_withCC_object_count = " << inside_withCC_object_count << endl;

        }
    };

    int inside_withCC::inside_withCC_object_count = 0;

    class withCC {
        static int withCC_object_count;
        inside_withCC IWITHCC;
    public:
        withCC(){
        out << "withCC constructions function is called" << endl;
        withCC_object_count++;
        out << "withCC(): withCC_object_count = " << withCC_object_count << endl;
        }
        withCC(const withCC&) {
            out << "withCC copy-constructions function is called" << endl;
            withCC_object_count++;
        out << "withCC(const withCC&): withCC_object_count = " << withCC_object_count << endl;

        }
    };

    int withCC::withCC_object_count = 0;

    class woCC {
        enum { bsz = 30 };
        char buf[bsz];
        static int woCC_object_count;
    public:
        woCC(const char* msg = 0) {
            memset(buf, 0, bsz);
            if(msg) strncpy(buf, msg, bsz);
            out << "woCC construction function is  called" << endl;
            woCC_object_count++;
            out << "woCC(): woCC_object_count: " << woCC_object_count << endl;
        }

        void print(const char* msg = 0) const {
            if(msg) out << msg << ": ";
            out << buf << endl;
        }
    };
    int woCC::woCC_object_count = 0;

    class composite {
        withCC WITHCC;
        woCC WOCC;
        static int composite_object_count;
    public:
        composite():WOCC("composite()") {
        out << "composite constructor function is called" << endl;
        composite_object_count++;
        out << "composite(): composite_object_count: " << composite_object_count << endl;
        }
        composite(const composite& cmp)
        {
            out << "composite copy-constructor function is called" << endl;
            composite_object_count++;
            out << "composite(composite&): composite_object_count:" << composite_object_count << endl;

        }

        void print(const char* msg = 0) {
            WOCC.print(msg);
        }
    };


    int composite::composite_object_count = 0;

    int main(int argc, char* argv[])
    {
        composite c;
        c.print("contents of c");
        out << "calling composite copy-constructor"
            << endl;
        composite c2 = c;
        c2.print("contents of c2");
        return 0;
    }

    autocc.out文件為:
    inside_withCC constructions function is called
    inside_withCC(): inside_withCC_object_count = 1
    withCC constructions function is called
    withCC(): withCC_object_count = 1
    woCC construction function is  called
    woCC(): woCC_object_count: 1
    composite constructor function is called
    composite(): composite_object_count: 1
    contents of c: composite()
    calling composite copy-constructor
    inside_withCC constructions function is called
    inside_withCC(): inside_withCC_object_count = 2
    withCC constructions function is called
    withCC(): withCC_object_count = 2
    woCC construction function is  called
    woCC(): woCC_object_count: 2
    composite copy-constructor function is called
    composite(composite&): composite_object_count:2
    contents of c2:

    可見:如果母對象中顯式的定義了X(X&),則子對象初始化時調用構造函數初始化。

    現在將程序改為:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    ofstream out("autocc.out");
    class inside_withCC {
            static int inside_withCC_object_count;
    public:
        inside_withCC(){
        out << "inside_withCC constructions function is called" << endl;
        inside_withCC_object_count++;
        out << "inside_withCC(): inside_withCC_object_count = " << inside_withCC_object_count << endl;
        }
        inside_withCC(const inside_withCC&) {
            out << "inside_withCC copy-constructions function is called" << endl;
            inside_withCC_object_count++;
        out << "inside_withCC(const withCC&): inside_withCC_object_count = " << inside_withCC_object_count << endl;

        }
    };

    int inside_withCC::inside_withCC_object_count = 0;

    class withCC {
        static int withCC_object_count;
        inside_withCC IWITHCC;
    public:
        withCC(){
        out << "withCC constructions function is called" << endl;
        withCC_object_count++;
        out << "withCC(): withCC_object_count = " << withCC_object_count << endl;
        }
        withCC(const withCC&) {
            out << "withCC copy-constructions function is called" << endl;
            withCC_object_count++;
        out << "withCC(const withCC&): withCC_object_count = " << withCC_object_count << endl;

        }
    };

    int withCC::withCC_object_count = 0;

    class woCC {
        enum { bsz = 30 };
        char buf[bsz];
        static int woCC_object_count;
    public:
        woCC(const char* msg = 0) {
            memset(buf, 0, bsz);
            if(msg) strncpy(buf, msg, bsz);
            out << "woCC construction function is  called" << endl;
            woCC_object_count++;
            out << "woCC(): woCC_object_count: " << woCC_object_count << endl;
        }

        void print(const char* msg = 0) const {
            if(msg) out << msg << ": ";
            out << buf << endl;
        }
    };
    int woCC::woCC_object_count = 0;

    class composite {
        withCC WITHCC;
        woCC WOCC;
        static int composite_object_count;
    public:
        composite():WOCC("composite()") {
        out << "composite constructor function is called" << endl;
        composite_object_count++;
        out << "composite(): composite_object_count: " << composite_object_count << endl;
        }
    /*    composite(const composite& cmp)
        {
            out << "composite copy-constructor function is called" << endl;
            composite_object_count++;
            out << "composite(composite&): composite_object_count:" << composite_object_count << endl;

        }
    */
        void print(const char* msg = 0) {
            WOCC.print(msg);
        }
    };


    int composite::composite_object_count = 0;

    int main(int argc, char* argv[])
    {
        composite c;
        c.print("contents of c");
        out << "calling composite copy-constructor"
            << endl;
        composite c2 = c;
        c2.print("contents of c2");
        return 0;
    }

    autocc.out的內容為:
    inside_withCC constructions function is called
    inside_withCC(): inside_withCC_object_count = 1
    withCC constructions function is called
    withCC(): withCC_object_count = 1
    woCC construction function is  called
    woCC(): woCC_object_count: 1
    composite constructor function is called
    composite(): composite_object_count: 1
    contents of c: composite()
    calling composite copy-constructor
    inside_withCC constructions function is called
    inside_withCC(): inside_withCC_object_count = 2
    withCC copy-constructions function is called
    withCC(const withCC&): withCC_object_count = 2
    contents of c2: composite()

    可見,如果母對象沒有定義X(X&),則母對象采用位拷貝,不掉用任何構造函數,子對象中如果有現式的定義X(X&)的,則調用之,否則采用位拷貝。


    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>