嵌套对象的copy-constructor

发表于:2007-05-26来源:作者:点击数: 标签:
当用一个已经初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用,如果你没有自定义拷贝构造函数的时候系统将会提供给一个默认的拷贝构造函数来完成这个过程。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.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)