|
#include <iostream>
using namespace std;
#include<string>
class Cstudent
{
private:
int mark;
char *name;
public:
Cstudent &operator+ (const Cstudent &stud);
Cstudent &operator= (const Cstudent &stud);
Cstudent(int mark, char *name);
Cstudent(){}
~Cstudent();
void out()
{
cout << name << "的分数为:" << mark << endl;
}
};
Cstudent& Cstudent::operator+ (const Cstudent &stud)
{
int i = strlen(stud.name) + 1;
this->name = new char[i];
memset(this->name, 0, strlen(this->name) + 1);
strcpy(this->name, stud.name);
this->mark += stud.mark;
return *this;
}
Cstudent& Cstudent::operator= (const Cstudent &stud)
{
this->name = new char[strlen(stud.name) + 1];
memset(this->name, 0, strlen(this->name) + 1);
strcpy(this->name, stud.name);
this->mark = stud.mark;
return *this;
}
Cstudent::Cstudent(int mark, char *name)
{
this->mark = mark;
this->name = new char[strlen(name) + 1];
memset(this->name, 0, strlen(this->name) + 1);
strcpy(this->name, name);
}
Cstudent::~Cstudent()
{
cout << "调用析构函数进行收尾工作" << this->mark << endl;
if (this->name)
{
delete[] this->name;
}
}
int main()
{
char str1[] = "超级霸霸强", str2[] = "超级无敌霸霸强";
Cstudent stud1(89, str1), stud2(87, str2), stud3;
stud3 = stud1 + stud2;
stud3.out();
}
请问一下,我这个程序为何还是会出现重复释放内存的现象?我在重载运算符中申请了内存的呀?因该可以对含有指针的类对象进行赋值与+法操作的呀? |
上一篇: codeblocks 如何设置Unicode编码,和_UNICODE的宏定义下一篇: MFC WPS Excel
|