1.拷贝构造函数与析构函数执行流程分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #define _CRT_SECURE_NO_WARNINGS
#include<iostream> #include<string.h> using namespace std;
class Student1 { public:
int age; char * name;
Student1() { cout << "空参数构造函数" << endl; }
Student1(char * name) :Student1(name, 99) { cout << "一个参数构造函数" << endl; }
Student1(char * name, int age) { cout << "二个参数构造函数" << endl;
this->name = (char *) malloc(sizeof(char * ) * 10); strcpy(this->name, name);
this->age = age; }
~Student1() { cout << "析构函数执行" << endl;
free(this->name); this->name = NULL; }
};
void mainT1() {
Student1 s1; Student1 s2 = s1;
cout << &s1 << endl; cout << &s2 << endl;
getchar(); }
|
2.拷贝构造函数与析构函数原理细节图研究
浅拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #define _CRT_SECURE_NO_WARNINGS
#include<iostream> #include<string.h> using namespace std;
class Student2 { public:
int age; char * name;
Student2() { cout << "空参数构造函数" << endl; }
Student2(char * name) :Student2(name, 99) { cout << "一个参数构造函数 this:" << this << endl; }
Student2(char * name, int age) { cout << "二个参数构造函数 this:" << this << endl;
this->name = (char *)malloc(sizeof(char *)* 10); strcpy(this->name, name);
this->age = age; }
~Student2() { cout << "析构函数执行 &this->name:" << &this->name << endl;
free(this->name); this->name = NULL; }
Student2(const Student2 & stu) {
cout << "拷贝构造函数 &stu:" << &stu << " this:" << this << endl;
this->name = stu.name;
} };
Student2 getStudent(char * name) { Student2 stu(name);
cout << "getStudent函数:" << &stu << endl;
return stu; }
void mainT3() { Student2 stu = getStudent("截拳道");
cout << "main函数:" << &stu << endl;
}
|
3.拷贝构造函数配合析构函数制作奔溃
拷贝构造函数中this的新地址,和旧地址都是指向堆区的同一个地址,所以多次调用会出现崩溃
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include<iostream> #include<string.h> using namespace std;
class Student2 { public:
int age; char * name;
Student2() { cout << "空参数构造函数" << endl; }
Student2(char * name) :Student2(name, 99) { cout << "一个参数构造函数 this:" << this << endl; }
Student2(char * name, int age) { cout << "二个参数构造函数 this:" << this << endl;
this->name = (char *)malloc(sizeof(char *)* 10); strcpy(this->name, name);
this->age = age; }
~Student2() { cout << "析构函数执行 &this->name:" << &this->name << endl;
free(this->name); this->name = NULL; }
Student2(const Student2 & stu) {
cout << "拷贝构造函数 &stu:" << &stu << " this:" << this << endl;
this->name = stu.name;
} };
void show(Student2 student2) { cout << &student2 << endl; }
int main() { Student2 student2; show(student2); show(student2); return 0; }
|
1 2 3 4 5 6
| 空参数构造函数 拷贝构造函数 &stu:0x62fd10 this:0x62fd20 0x62fd20 析构函数执行 &this->name:0x62fd28
Process finished with exit code -1073740940 (0xC0000374)
|
4.深拷贝解决奔溃,并分析原理
深拷贝:
如果类成员变量中有堆成员,就需要重写拷贝构造函数,实现深拷贝。默认的拷贝构造函数是浅拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #define _CRT_SECURE_NO_WARNINGS
#include<iostream> #include<string.h> using namespace std;
class Student { public:
int age; char * name;
Student() { cout << "空参数构造函数" << endl; }
Student(char * name) :Student(name, 99) { cout << "一个参数构造函数 this:" << (int)this << endl; }
Student(char * name, int age) { cout << "二个参数构造函数 this:" << (int)this << endl;
this->name = (char *)malloc(sizeof(char *)* 10); strcpy(this->name, name);
this->age = age; }
~Student() { cout << "析构函数执行 &this->name:" << (int)this->name << endl;
free(this->name); this->name = NULL; }
Student(const Student & stu) {
cout << "拷贝构造函数 &stu:" << (int)&stu << " this:" << (int)this << endl;
this->name = (char *)malloc(sizeof(char *)* 10); strcpy(this->name, name);
this->age = stu.age;
cout << "拷贝构造函数2 this->name:" << ((int) this->name) << " stu.name:" << (int)stu.name << endl;
}
};
void showStudent(Student stu) { cout << "showStudent函数:" << (int)&stu << " " << stu.name << "," << stu.age<< endl; }
void main() { Student stu("刘奋", 31);
showStudent(stu);
showStudent(stu);
getchar(); }
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者