64,701
社区成员
发帖
与我相关
我的任务
分享
MyArry& MyArry::operator=(const MyArry& arry2)
有两个参数,都是MyArry类型对象。 A a1 = b;中,在执行的时候,a1什么也不是,它还不是一个A类型的对象,所以不符合operator=。delete a1时调用了析构函数,那是因为执行完 A a1 = b;后, a1 是一个 A类型对象;而 A* p = NULL;p本身是存在的,所以可以调用funcion,如果不出意外的话 *p.function()应该会出错。[/quote]MyArry& MyArry::operator=(const MyArry& arry2)
有两个参数,都是MyArry类型对象。 A a1 = b;中,在执行的时候,a1什么也不是,它还不是一个A类型的对象,所以不符合operator=。delete a1时调用了析构函数,那是因为执行完 A a1 = b;后, a1 是一个 A类型对象;而 A* p = NULL;p本身是存在的,所以可以调用funcion,如果不出意外的话 *p.function()应该会出错。[/quote]MyArry& MyArry::operator=(const MyArry& arry2)
有两个参数,都是MyArry类型对象。 A a1 = b;中,在执行的时候,a1什么也不是,它还不是一个A类型的对象,所以不符合operator=。delete a1时调用了析构函数,那是因为执行完 A a1 = b;后, a1 是一个 A类型对象;而 A* p = NULL;p本身是存在的,所以可以调用funcion,如果不出意外的话 *p.function()应该会出错。MyArry& MyArry::operator+=(const MyArry& arry2) {
if (this==&arry2)
{
return *this;
}
if (this->space!=NULL)
{
delete[] this->space;
this->space = NULL;
}
delete[] this->space;
this->space = new int[arry2.len];
this->len = arry2.len;
memcpy(this->space, arry2.space, arry2.len);
return *this;
}
还是不行呀
[/quote]
他这个弄错了,应该是 Myarry& Myarry::operator=(const Myarry&);
[/quote]
你这个是对 MyArry arry5;
arry5 = arry4;
用的。 MyArry arry4 = *arry1;还是调用的拷贝构造方法。不是重载的“=”MyArry& MyArry::operator+=(const MyArry& arry2) {
if (this==&arry2)
{
return *this;
}
if (this->space!=NULL)
{
delete[] this->space;
this->space = NULL;
}
delete[] this->space;
this->space = new int[arry2.len];
this->len = arry2.len;
memcpy(this->space, arry2.space, arry2.len);
return *this;
}
还是不行呀
[/quote]
他这个弄错了,应该是 Myarry& Myarry::operator=(const Myarry&);
MyArry& MyArry::operator+=(const MyArry& arry2) {
if (this==&arry2)
{
return *this;
}
if (this->space!=NULL)
{
delete[] this->space;
this->space = NULL;
}
delete[] this->space;
this->space = new int[arry2.len];
this->len = arry2.len;
memcpy(this->space, arry2.space, arry2.len);
return *this;
}
还是不行呀