中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
嵌套对象的copy-constructor
作者:未知 时间:2005-09-13 23:28 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:嵌套对象的copy-constructor
当用一个已经初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用,如果你没有自定义拷贝构造函数的时候系统将会提供给一个默认的拷贝构造函数来完成这个过程。Copy构造函数有一个参数,这个参数是一个该类的对象。例如CString的构造函数CString( const CString& stringSrc )就是一个Copy构造函数。

代码:
#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 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&)的,则调用之,否则采用位拷贝。


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有