我想通过复制构造函数创建我的类的实例的深层副本,但我无法弄清楚,如何编写它..在这一刻,当我调用复制构造函数,程序不会崩溃,但是当我想要对实例进行任何操作时(例如,打印数组,向其中添加一些项目等),那么程序崩溃...复制构造函数中的深拷贝的问题
有人请告诉我,如何正确写入?它的驾驶我疯狂又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的实例?也许问题在别的地方。 –