浅拷贝
class Student{
public:
int age;
char * name;
Student(){ cout << "空构造函数" << endl;}
Student(char * name): Student(name,age){
cout << "一个参数构造函数this:" << (int)this << endl;}
Student(char * name, int age){
cout << "两个参数构造函数this:" << (int)this << endl;
this->name = (char *)malloc(sizeof (char *) * 10);
this->age = age;
Strcpy(this->name,name);
}
Student (const Student & student) {
this->name = student.name;
this->age = student.age-10;
cout << "拷贝函数:this->name" << (int)this->name << endl;
}
~Student(){
cout << "析构函数执行this:" << (int)this << endl;
free(this->name);
this->name = NULL;
}
};
int main(){
Stnedent s1;
Student s2;
cout << &s1 << endl;
cout << &s2 << endl;
Stnedent s1;
Student s2 = s1;
cout << &s1 << endl;
cout << &s2 << endl;
Student (const Student & student) {
cout << "拷贝函数" << endl;
this->name = student.name;
this->age = student.age-10;
}
return 0;
}
Student getStudent(char * name){
Student stu(name);
cout << "getStudnet函数:" << &stu << endl;
return stu;
}
void main(){
Student stu = getStudent("截拳道");
cont << "main"函数 << &stu << endl;
}
深拷贝
void showStudent(Student stu){
cout << "showStudent函数" << (int)&stu << stu.name << "," << stu.age << endl;
}
void main(){
Student stu("六分", 22);
showStudent(stu);
showStudent(stu);
this->name = (char *)malloc(sizeof (char *) * 10);
Strcpy(this->name,name);
}