吉行天下, 力挽狂澜

----------吉豕皮, 力一女共筑----------
随笔 - 35, 文章 - 2, 评论 - 3, 引用 - 0
数据加载中……

C++对象的拷贝与赋值操作

拷贝构造函数,顾名思义,等于拷贝 + 构造。它肩负着创建新对象的任务,同时还要负责把另外一个对象拷贝过来。比如下面的情况就调用拷贝构造函数:

CString str = strOther;

 

赋值操作则只含有拷贝的意思,也就是说对象必须已经存在。比如下面的情况会调用赋值操作。

str = strOther;

 

不过有的对象是隐式的,由编译器产生的代码创建,比如函数以传值的方式传递一个对象时。由于看不见相关代码,所以不太容易明白。不过我们稍微思考一下,就会想到,既然是根据一个存在的对象拷贝生成新的对象,自然是调用拷贝构造函数了。

 

两者实现时有什么差别呢?我想有人会说,没有差别。呵,如果没有差别,那么只要实现其中一个就行了,何必要两者都实现呢?不绕圈子了,它们的差别是:

 

拷贝构造函数对同一个对象来说只会调用一次,而且是在对象构造时调用。此时对象本身还没有构造,无需要去释放自己的一些资源。而赋值操作可能会调用多次,你在拷贝之前要释放自己的一些资源,否则会造成资源泄露。

 

明白了这些道理之后,我们不防写个测试程序来验证一下我们的想法:

#include  < stdio.h >

#include 
< stdlib.h >

#include 
< string .h >

 

class  CString

{   

public :

    CString();

    CString(
const   char *  pszBuffer);

    
~ CString();

    CString(
const  CString &  other);

    
const  CString &   operator = ( const  CString &  other);

 

private :

    
char *  m_pszBuffer;;

}
;  

 

CString::CString()

{

    printf(
" CString::CString\n " );

    m_pszBuffer 
=  NULL;

 

    
return

}
   

        

CString::CString(
const   char *  pszBuffer)

{

    printf(
" CString::CString(const char* pszBuffer)\n " );

    m_pszBuffer 
=  pszBuffer  !=  NULL  ?  strdup(pszBuffer) : NULL;

 

    
return ;

}


CString::
~ CString()

{

    printf(
" %s\n " , __func__);

    delete m_pszBuffer;

    m_pszBuffer 
=  NULL;

 

    
return ;

}


 

CString::CString(
const  CString &  other)

{

    
if ( this   ==   & other)

    
{

        
return ;

    }


 

    printf(
" CString::CString(const CString& other)\n " );

    m_pszBuffer 
=  other.m_pszBuffer  !=  NULL  ?  strdup(other.m_pszBuffer) : NULL;

}


 

const  CString &  CString:: operator = ( const  CString &  other)

{

    printf(
" const CString& CString::operator=(const CString& other)\n " );

 

    
if ( this   ==   & other)

    
{

        
return   * this ;

    }


 

    
if (m_pszBuffer  !=  NULL)

    
{

        free(m_pszBuffer);

        m_pszBuffer 
=  NULL;

    }


         m_pszBuffer 
=  other.m_pszBuffer  !=  NULL  ?  strdup(other.m_pszBuffer) : NULL;

 

    
return   * this ;

}


 

void  test(CString str)

{

    CString str1 
=  str;

 

    
return ;

}


 

int  main( int  argc,  char *  argv[])

{

    CString str;

    CString str1 
=   " test " ;

    CString str2 
=   str1;

    str1 
=  str;

 

    CString str3 
=  str3;

 

    test(str);

 

    
return   0 ;

}


posted on 2006-11-20 18:22 吉豕皮 阅读(158) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。