淺談C++對象的拷貝與賦值操作
我發(fā)現(xiàn)一些同事在用C++編寫一個(gè)類時(shí),知道什么時(shí)候需要實(shí)現(xiàn)拷貝構(gòu)造函數(shù)和賦值操作,但不知道什么時(shí)候拷貝構(gòu)造函數(shù)被調(diào)用,什么時(shí)候賦值操作被調(diào)用,甚至把二者混為一談。
要弄明白這個(gè)問題,最簡單的做法莫過于寫個(gè)測試程序試一下。不過那樣做也未必是好辦法,實(shí)驗(yàn)的結(jié)果往往導(dǎo)致以偏概全的結(jié)論。不如好好想一下,弄清楚其中的原理,再去寫程序去驗(yàn)證也不遲。
拷貝構(gòu)造函數(shù),顧名思義,等于拷貝 + 構(gòu)造。它肩負(fù)著創(chuàng)建新對象的任務(wù),同時(shí)還要負(fù)責(zé)把另外一個(gè)對象拷貝過來。比如下面的情況就調(diào)用拷貝構(gòu)造函數(shù):
- cstring str = strother;
賦值操作則只含有拷貝的意思,也就是說對象必須已經(jīng)存在。比如下面的情況會調(diào)用賦值操作。
- str = strother;
不過有的對象是隱式的,由編譯器產(chǎn)生的代碼創(chuàng)建,比如函數(shù)以傳值的方式傳遞一個(gè)對象時(shí)。由于看不見相關(guān)代碼,所以不太容易明白。不過我們稍微思考一下,就會想到,既然是根據(jù)一個(gè)存在的對象拷貝生成新的對象,自然是調(diào)用拷貝構(gòu)造函數(shù)了。
兩者實(shí)現(xiàn)時(shí)有什么差別呢?我想有人會說,沒有差別。呵,如果沒有差別,那么只要實(shí)現(xiàn)其中一個(gè)就行了,何必要兩者都實(shí)現(xiàn)呢?不繞圈子了,它們的差別是:
拷貝構(gòu)造函數(shù)對同一個(gè)對象來說只會調(diào)用一次,而且是在對象構(gòu)造時(shí)調(diào)用。此時(shí)對象本身還沒有構(gòu)造,無需要去釋放自己的一些資源。而賦值操作可能會調(diào)用多次,你在拷貝之前要釋放自己的一些資源,否則會造成資源泄露。
明白了這些道理之后,我們不防寫個(gè)測試程序來驗(yàn)證一下我們的想法:
- #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;
- }
希望對你有幫助。
【編輯推薦】