大牛们过来看看我的析构函数需要怎么改?
#include<iostream>using namespace std;
//=号操作符重载 只能是成员函数 不能用全局函数
class A
{
public:
char *p;
int a;
int jishu;
public:
static int Aa;
public:
A(char *_p = "我爱你!", int _a = 0)
{
p = _p;
a = _a;
Aa++;
jishu = Aa;
}
void Display()
{
cout << "第" << jishu << "个对象的值:\n";
cout << "p = " << p << " ." << " a = " << a << endl;
cout << "第"<<jishu<<"个对象的p的地址:"<<&p << endl;
}
A operator=(A& Object)
{
if (this->p != NULL)
{
delete[]p;
}
int i;
this->p = new char[strlen(Object.p) + 1];
for (i = 0; i <= strlen(Object.p); i++)
this->p[i] = Object.p[i];
this->p[i] = '\0';
this->jishu = Aa;
return (*this);
}
~A()
{
if (p != NULL)
delete[]p;
}
};
int A::Aa = 0;
int main()
{
A Object1, Object2("我的", 10);
Object1.Display();
Object2.Display();
A Object3 = Object2;
Object3.Display();
cout << A::Aa << endl;
A Object4;
Object4.Display();
system("pause");
return 0;
}
能运行 但是可能到析构函数的时候窗体都停止了 还响了下声音.