C++拷贝赋值operator=_数据

拷贝赋值:两个对象之间的拷贝

默认拷贝赋值导致错误

#include <iostream>
class A {
public:
A(int i=10){
m_pi = new int(i);
ii = i;

}
A(const A& that) {
std::cout << "深拷贝构造函数" << std::endl;
m_pi = new int;
*m_pi = *that.m_pi;
}
void print(void) {
std::cout << ii << std::endl;
}
~A(void) {

std::cout << "xigouhansu:"<<this << std::endl;
delete m_pi;
}
private:
int* m_pi;
int ii;

};

int main()
{
A a(100);
A b;
b.print();
//b.operator=(a);
b = a;//拷贝赋值;与b.operator=(a)等价
//拷贝赋值:两个对象已经存在
//编译器其实执行函数:b.operator=(a);
//如果程序没写这个函数,会执行默认的,在执行析构函数时导致程序中断
//中断原因:默认的拷贝赋值是直接拷贝各个成员变量值;由于m_pi是指针,a和b指向同一个堆区数据,不能delete两次
//所以当存在指针类型的成员变量时,要自己写b.operator=(a)函数
b.print();


}



自写拷贝赋值函数

#include <iostream>
class A {
public:
A(int i=10){
m_pi = new int(i);
ii = i;

}
A(const A& that) {
std::cout << "senkaobei" << std::endl;
m_pi = new int;
*m_pi = *that.m_pi;
}
A& operator=(const A&that) { //自写拷贝赋值函数
if (&that != this) { //防止自赋值
*m_pi = *that.m_pi; //拷贝指针的数据
ii = that.ii;
}
return *this; //返回自身引用
}
void print(void) {
std::cout << ii << std::endl;
std::cout << *m_pi << std::endl;
}
~A(void) {

std::cout << "xigouhansu:"<<this << std::endl;
delete m_pi;
}
private:
int* m_pi;
int ii;

};

int main()
{
A a(100);
A b;

b.print();
b = a; //拷贝赋值
std::cout << &a << std::endl;
std::cout << &b << std::endl;
b.print();


}