我想通過複製構造函數創建我的類的實例的深層副本,但我無法弄清楚,如何編寫它..在這一刻,當我調用複製構造函數,程序不會崩潰,但是當我想要對實例進行任何操作時(例如,打印數組,向其中添加一些項目等),那麼程序崩潰...複製構造函數中的深拷貝的問題
有人請告訴我,如何正確寫入?它的駕駛我瘋狂又O_O
struct DbChange {
const char* date;
const char* street;
const char* city;
};
class DbPerson {
public:
DbPerson(void);
const char* id;
const char* name;
const char* surname;
DbChange * change;
int position;
int size;
};
DbPerson::DbPerson() {
position = 0;
size = 1000;
change = new DbChange[1000];
}
class Register {
public:
// default constructor
Register(void);
int size;
int position;
DbPerson** db;
//copy constructor
Register(const Register& other) : db() {
db= new DbPerson*[1000];
std::copy(other.db, other.db + (1000), db);
}
};
int main(int argc, char** argv) {
Register a;
/*
* put some items to a
*/
Register b (a);
a . Print(); // now crashes
b . Print(); // when previous line is commented, then it crashes on this line...
return 0;
}
什麼是你的空格鍵/ Tab鍵的問題?另外'註冊(void);'應該是'Register();' –
你可以說'std :: string'男孩和女孩嗎?我知道你可以! –
您是否嘗試過'a.Print()'而不創建b的實例?也許問題在別的地方。 –