一个类至少有4个默认函数:

code:
#include
using namespace std;
class Horse
{
private:
int *m_age;
public:
Horse(int ref_age)
{
m_age = new int(ref_age);
}
Horse(const Horse& ref_h)
{
m_age = new int(*ref_h.m_age);
//m_age = ref_h.m_age; //默认的构造函数是这种实现方式,简单的赋值操作,在存在指针时,只是将指针简单赋值,指针指向的堆区空间并不进行拷贝。
cout << "拷贝, " << m_age << ", " << ref_h.m_age << ", age: " << * ref_h.m_age << endl;
}
Horse& operator=(const Horse& ref_h) // 返回自身的引用,链式编程,实现连等操作
{
if (this == &ref_h) // 如果是自身赋值,如h1=h1,直接返回
cout << "自身赋值" << endl;
return *this;
// 如果对象已经有开辟了空间,先进行释放
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
//m_age = ref_h.m_age; //默认的是这种实现方式,简单的赋值操作,在存在指针时,只是将指针简单赋值,指针指向的堆区空间并不进行拷贝。
m_age = new int(*ref_h.m_age); // 深拷贝
cout << "=," << m_age << ", " << ref_h.m_age << ", age: " << * ref_h.m_age << endl;
return *this;
}
~Horse()
{
if (m_age != NULL)
{
cout << "析构:" << m_age << endl;
delete m_age;
m_age = NULL;
}
}
void show_info()
{
cout << "age: " << * m_age << endl;
}
};
// 对拷贝构造函数的测试
void test01()
{
Horse h1(0);
h1.show_info();
Horse h2(h1);
h2.show_info();
}
// 对=操作符的测试
void test02()
{
Horse h1(88);
Horse h2(2);
Horse h3(5);
h3 = h2 = h1;
h1.show_info();
h2.show_info();
h3.show_info();
}
void main()
{
test01();
cout << "************************" << endl;
test02();
system("pause");
}
result:
age: 0
拷贝, 0000024C16116510, 0000024C16116A50, age: 0
age: 0
析构:0000024C16116510
析构:0000024C16116A50
************************
=,0000024C16116150, 0000024C16116750, age: 88
=,0000024C161164D0, 0000024C16116150, age: 88
age: 88
age: 88
age: 88
析构:0000024C161164D0
析构:0000024C16116150
析构:0000024C16116750