4.5.4复制运算符
#include <iostream>
using namespace std;
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)
{
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);
p2 = p1;
cout << *p1.m_age << endl;
cout << *p2.m_age << endl;
}
int main4_5_4()
{
test01();
system("pause");
return 0;
}
4.5.3递增运算符
#include <iostream>
using namespace std;
class myinteger
{
friend ostream & operator<<(ostream &cout, myinteger myint);
public:
myinteger()
{
m_num = 0;
}
myinteger& operator++()
{
m_num++;
return *this;
}
myinteger operator++(int)
{
myinteger temp;
temp = *this;
m_num++;
return temp;
}
private:
int m_num;
};
ostream & operator<<(ostream &cout, myinteger myint)
{
cout << myint.m_num;
return cout;
}
void test01()
{
myinteger myint;
cout << ++myint << endl;
}
void test02()
{
myinteger myint;
cout << myint++ << endl;
}
int main()
{
test02();
system("pause");
return 0;
}