如果一個類裡沒有寫出任何構造函數,編譯器會替你生成一個「什麼也不做」的空殼版本:
test::test()
{}
若你不想讓編譯器代勞,自己實現一個默認構造函數即可;也可以帶上參數,做更靈活的初始化:
test::test()
{
...........;
}
test::test(const char* tin)
{
...........;
}
複製構造函數負責把一個已存在的對象「複製」到一個新建對象中,原型形如:
class_name(const class_name &);
以下幾種寫法都會觸發它:
test my1(my); 顯式調用,my1 是 my 的副本test my1 = test(my); 顯式調用,my1 是 my 的副本test my1 = my; 隱式調用,my1 是 my 的副本void tf1(test t1); 按值傳參時生成臨時對象test *p = new test(my);一句話總結:只要程式需要生成對象副本,編譯器就會走複製構造函數。而「副本」是個陷阱——若類中成員是指針,複製出來的對象會指向同一塊記憶體。此時一旦各自析構,同一片空間就被釋放兩次;同時依賴計數的邏輯也會徹底失效。
如果類裡有靜態成員,例如:
private: static int num_s;
需要在類外完成定義與初始化:
int charin::num_s = 0;
它本質上就是靜態全域變數,同一塊記憶體被所有對象共享,多次調用讀到的自然是同一份數據。
先看頭文件 c2.h:
#include <iostream>
using namespace std;
class charin
{
private:
static int num_s;
char* sin;
int len;
public:
charin(void);
charin(const char *);
//charin(charin &);
~charin(void);
friend std::ostream & operator<<(std::ostream &, const charin &);
};
實作檔 cf.cpp:
#include <iostream>
#include <string.h>
#include "c2.h"
using namespace std;
int charin::num_s = 0;
charin::charin(void)
{
len = strlen("study c++") + 1;
cout << len << " mem alloc!" << endl;
sin = new char[len];
strcpy(sin, "study c++");
num_s++;
cout << sin << " in " << num_s << endl;
}
charin::charin(const char* sorstr)
{
len = strlen(sorstr) + 1;
cout << len << " bytes mem alloc!" << endl;
sin = new char[len];
strcpy(sin, sorstr);
num_s++;
cout << sin << " in " << num_s << endl;
}
charin::~charin(void)
{
num_s--;
cout << len << " bytes mem release!" << endl;
cout << sin << " out " << num_s << endl;
delete [] sin;
}
std::ostream & operator<<(std::ostream & os, const charin & cin)
{
os << cin.sin;
return os;
}
主程式 main.cpp:
#include <iostream>
#include "c2.h"
using namespace std;
void chmi(charin);
int main(void)
{
const char *testc = "test123";
charin t1;
cout << "test" << endl;
charin t2(testc);
charin t3 = t2; //error
chmi(t2); //error
return 0;
}
//error:sin 指標指向同一片記憶體
void chmi(charin a)
{
cout << "test here: " << a << endl;
}
在 Linux 上用 g++ 編譯執行,輸出如下:
gaopeng@bogon:~/CPLUSPLUS/part11/c2$ ./a.out 10 mem alloc! study c++ in 1 test 8 bytes mem alloc! test123 in 2 test here: test123 8 bytes mem release! out 1 *** Error in `./a.out': double free or corruption (fasttop): 0x00000000021c1030 *** Aborted (core dumped)
錯誤訊息 double free or corruption 說得很直白:同一塊記憶體被重複釋放。而且 num_s 這個計數器也亂了——副本析構時把別人的份也扣掉了。
解法是自己定義複製構造函數,為指標成員重新申請記憶體,而不是複製指標的指向。先在 c2.h 中宣告:
#include <iostream>
using namespace std;
class charin
{
private:
..........
public:
..........
charin(charin &); //加入
.........
};
再在 cf.cpp 中實作:
charin::charin(charin &in)
{
len = in.len;
sin = new char[len];
strcpy(sin, in.sin);
num_s++;
cout << len << " bytes mem alloc!" << endl;
cout << sin << " deep copy in " << num_s << endl;
}
重新編譯執行:
10 mem alloc! study c++ in 1 test 8 bytes mem alloc! test123 in 2 8 bytes mem alloc! test123 deep copy in 3 8 bytes mem alloc! test123 deep copy in 4 test here: test123 8 bytes mem release! test123 out 3 8 bytes mem release! test123 out 2 8 bytes mem release! test123 out 1 10 bytes mem release! study c++ out 0
每個對象各自持有獨立緩衝區,計數器一加一減回到零,程式乾淨退出。這就是深拷貝的價值所在。