对象中的指针
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(int id1, char *name1, int score1);
Student();
~Student();
friend void Aprint(Student *p, int size);
friend void bubblesort(Student *p, int size);
static void show_ave();
private:
int id;
char *name;
int score;
static int sum;
static int count;
static double ave;
};
int Student::sum = 0;
int Student::count = 0;
double Student::ave = 0.0;
Student::Student(int id1, char *name1, int score1)
{
id = id1;
name = new char[strlen(name1) + 1];
strcpy(name, name1);
score = score1;
++count;
sum += score1;
ave = sum / count;
}
Student::Student()
{
id = 0;
name = "UNKNOWN";
score = 0;
++count;
sum += score;
ave = sum / count;
}
Student::~Student()
{
delete []name;
cout << "destructor called!" << endl;
}
void Aprint(Student *p, int size)
{
int i;
for (i = 0; i < size; i++) {
cout << "ID: " << p->id << "Name: " << p->name << "Score: " << p->score <<endl;
p++;
}
}
void Student::show_ave()
{
cout << "SUM: " << sum << endl;
cout << "Count:" << count << endl;
cout << "The average of score :" << ave << endl;
}
void bubblesort(Student *p, int size)
{
int i, j;
Student temp;
for (i = 0; i < size; i++) {
for (j = 0; j < size - i - 1; j++) {
if (p[j].score > p[j + 1].score) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
int main()
{
Student *stu;
Student S[3] = {Student(1, "ly", 99),
Student(2, "cj", 87),
Student(3, "dw", 67)};
stu = S;
Aprint(stu,3);
Student::show_ave();
cout << "After sort:" << endl;
bubblesort(stu, 3);
Aprint(stu, 3);
return 0;
}
大家看看这段代码,简单的练习,总是在排完序后出错,中间会无故调用析构函数,还有指针复制时作怪,怎么解决?
[解决办法]
1.缺少了一个拷贝构造函数,默认的拷贝构造函数会直接复制内存块
2.这么初始化 name = "UNKNOWN"; 与对应的析构delete []name; 100%的出错,不配对
[解决办法]
class Student
{
public:
// 第一个参数不该就加上const,不然用字面量作为实参时编译器会报警告的
//Student(int id1, char *name1, int score1);
Student(int id1, const char *name1, int score1);
Student();
~Student();
friend void Aprint(Student *p, int size);
friend void bubblesort(Student *p, int size);
static void show_ave();
// 对于包含动态分配数据的class,必须提供行为正确的赋值运算符,或者明确禁止
Student& operator=(const Student& other)
{
char* tmp = new char[strlen(other.name) + 1];
strcpy(tmp, other.name);
delete name;
name = tmp;
id = other.id;
score = other.score;
return *this;
}
private:
int id;
char *name;
int score;
static int sum;
static int count;
static double ave;
};
int Student::sum = 0;
int Student::count = 0;
double Student::ave = 0.0;
Student::Student(int id1, const char *name1, int score1)
{
id = id1;
name = new char[strlen(name1) + 1];
strcpy(name, name1);
score = score1;
++count;
sum += score1;
ave = sum / count;
}
Student::Student()
{
id = 0;
// 这里错了,不能这么直接赋值,必须申请内存了然后再拷贝
//name = "UNKNOWN";
name = new char[strlen("UNKNOWN") + 1];
strcpy(name, "UNKNOWN");
score = 0;
++count;
sum += score;
ave = sum / count;
}
Student::~Student()
{
delete[] name;
cout << "destructor called!" << endl;
}
void Aprint(Student *p, int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << "ID: " << p->id << "Name: " << p->name << "Score: " << p->score << endl;
p++;
}
}
void Student::show_ave()
{
cout << "SUM: " << sum << endl;
cout << "Count:" << count << endl;
cout << "The average of score :" << ave << endl;
}
void bubblesort(Student *p, int size)
{
int i, j;
Student temp;
for (i = 0; i < size; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (p[j].score > p[j + 1].score)
{
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
// 函数结束时调用tmp的析构函数,正常
}
int main()
{
Student *stu;
Student S[3] = { Student(1, "ly", 99), Student(2, "cj", 87), Student(3, "dw", 67) };
stu = S;
Aprint(stu, 3);
Student::show_ave();
cout << "After sort:" << endl;
bubblesort(stu, 3);
Aprint(stu, 3);
// main函数结束时调用S中各个Student的析构函数
return 0;
}
#include<string>
class Student
{
public:
Student(int id1, char *name1, int score1);
Student();
Student(const Student &_info);
~Student();
Student& operator=(const Student &_info);
friend void Aprint(Student *p, int size);
friend void bubblesort(Student *p, int size);
static void show_ave();
private:
int id;
char *name;
int score;
static int sum;
static int count;
static double ave;
};
int Student::sum = 0;
int Student::count = 0;
double Student::ave = 0.0;
Student::Student(int id1, char *name1, int score1)
{
id = id1;
name = new char[strlen(name1) + 1];
strcpy(name, name1);
score = score1;
++count;
sum += score1;
ave = sum / count;
}
Student::Student(const Student &_info)
{
this->id = _info.id;
if (name)
{
delete name;
}
this->name = new char[strlen(_info.name) + 1];
strcpy(this->name, _info.name);
this->name = _info.name;
this->score = _info.score;
}
Student& Student::operator=(const Student &_info)
{
if (this == &_info)
{
return *this;
}
this->id = _info.id;
strcpy(this->name, _info.name);
this->score = _info.score;
return *this;
}
Student::Student()
{
id = 0;
name = new char[10];
memset(name, 0, 10);
score = 0;
++count;
sum += score;
ave = sum / count;
}
Student::~Student()
{
if (name)
{
delete name;
}
cout << "destructor called!" << endl;
}
void Aprint(Student *p, int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << "ID: " << p->id << "Name: " << p->name << "Score: " << p->score <<endl;
p++;
}
}
void Student::show_ave()
{
cout << "SUM: " << sum << endl;
cout << "Count:" << count << endl;
cout << "The average of score :" << ave << endl;
}
void bubblesort(Student *p, int size)
{
int i, j;
Student temp;
for (i = 0; i < size; i++)
{
for (j = i+1; j < size; j++)
{
if (p[j].score > p[j + 1].score)
{
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
int main()
{
Student *stu;
Student S[3] = {Student(1, "ly", 99),
Student(2, "cj", 87),
Student(3, "dw", 67)};
stu = S;
Aprint(stu,3);
stu = S;
Student::show_ave();
cout << "After sort:" << endl;
bubblesort(stu, 3);
Aprint(stu, 3);
stu = S;
return 0;
}
改好的,添加了一个拷贝构造函数、一个赋值函数。并修改了构造函数Student::Student()
{
name = new char[10];
memset(name, 0, 10);