轉帖|其它|編輯:郝浩|2010-07-19 10:37:03.000|閱讀 594 次
概述:我發現一些同事在編寫一個類時,知道什么時候需要實現拷貝構造函數和賦值操作,但不知道什么時候拷貝構造函數被調用,什么時候賦值操作被調用,甚至把二者混為一談。要弄明白這個問題,最簡單的做法莫過于寫個測試程序試一下。不過那樣做也未必是好辦法,實驗的結果往往導致以偏概全的結論。不如好好想一下,弄清楚其中的原理,再去寫程序去驗證也不遲。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
我發現一些同事在編寫一個類時,知道什么時候需要實現拷貝構造函數和賦值操作,但不知道什么時候拷貝構造函數被調用,什么時候賦值操作被調用,甚至把二者混為一談。
要弄明白這個問題,最簡單的做法莫過于寫個測試程序試一下。不過那樣做也未必是好辦法,實驗的結果往往導致以偏概全的結論。不如好好想一下,弄清楚其中的原理,再去寫程序去驗證也不遲。
拷貝構造函數,顧名思義,等于拷貝 + 構造。它肩負著創建新對象的任務,同時還要負責把另外一個對象拷貝過來。比如下面的情況就調用拷貝構造函數:
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__);
if(m_pszBuffer != NULL)
{
free(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;
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:csdn博客