有深 拷贝 加运算符重载 就报错了
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Tank
{
public:
friend Tank operator+(const Tank &t1, const Tank &t2);
Tank(int code, int count):m_iCode(code),m_iCount(count)
{
cout <<"tank()"<<endl;
m_p = new char[100];
}
~Tank()
{
cout <<"~tank()"<<endl;
delete []m_p;
m_p = NULL;
}
Tank(const Tank& t);
void getDate()
{
cout << m_iCode <<endl;
cout << m_iCount <<endl;
}
private:
int m_iCode;
int m_iCount;
char *m_p;
};
Tank operator+(const Tank &t1, const Tank &t2)
{
Tank temp(0,0);
temp.m_iCode = t1.m_iCode + t2.m_iCode;
temp.m_iCount = t1.m_iCount + t2.m_iCount;
return temp;
}
Tank::Tank(const Tank& t)
{
cout << "Tank copy()" << endl;
m_iCode = t.m_iCode + 1;
m_iCount = t.m_iCount;
m_p = new char[100];
if(m_p != NULL)
cout <<"copy right"<< endl;
}
int main()
{
Tank t1(10,20);
Tank t2(20,30);
Tank t3(0,0);
t3 = t1 + t2;
t1.getDate();
t2.getDate();
t3.getDate();
return 0;
}