//4.5.4 ��ֵ��������� //C++���������ٸ�һ��������4������ //1��Ĭ�Ϲ��캯��(�Σ�������Ϊ��) //2��Ĭ����������(�Σ�������Ϊ��) //3��Ĭ�Ͽ������캯���������Խ���ֵ���� //4����ֵ�����operator= , �����Խ���ֵ���� //�������������ָ�����������ֵ����ʱҲ�������dz�������� #if(0) #include <iostream> class Person { public: Person(int age) { m_Age = new int(age); } ~Person() { if (m_Age != NULL) { delete m_Age; m_Age = NULL; } } //���� ��ֵ����� Person& operator=(Person &p) { //���������ṩ��dz���� //m_Age = p.m_Age; //Ӧ�����ж��Ƿ��������ڶ�������������ͷŸɾ�������� if (m_Age != NULL) { delete m_Age; m_Age = NULL; } //��� m_Age = new int(*p.m_Age); return *this; } int* m_Age; }; void test01() { Person p1(18); Person p2(20); Person p3(30); p3 = p2 = p1; //��ֵ������� std::cout << "p1������Ϊ��" << *p1.m_Age << std::endl; std::cout << "p2������Ϊ��" << *p2.m_Age << std::endl; std::cout << "p3������Ϊ��" << *p3.m_Age << std::endl; } int main() { test01(); //int a = 10; //int b = 20; //int c = 30; //c = b = a; //std::cout << "a = " << a << std::endl; //std::cout << "b = " << b << std::endl; //std::cout << "c = " << c << std::endl; system("pause"); return 0; } #endif