《C++学习》构造函数和析构函数
一、对象的初始化和清理
对象的初始化和清理是两个非常重要的安全问题,一个对象或者变量没有初始状态,对其使用后果是未知的,同样的,使用完一个对象或者变量,没有及时清理,也会造成一定的安全问题。C++的构造函数和析构函数就是解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。对象初始化和清理工作是编译器强制我们做的事情,因此如果哟我们不提供构造和析构,编译器也会提供,只不过提供的两个函数是空实现。
二、构造函数
1、作用
构造函数主要作用于创建对象时为对象的成员属性赋值,由编译器自动调用,无需自己动手调用
2、语法:类名(){}
(1)没有返回值,也不写void(函数如果是void,则无返回值,如果有类型,则需要返回值)
(2)函数名称与类名相同
(3)构造函数可以有参数,因此可以发生重载
(4)程序在调用对象的时候会自动调用构造,无需自己调用,而且只会调用一次
3、代码演示
#include<iostream>
using namespace std;
class Person {
public:
//构造函数 进行初始化操作
Person() {
cout << "Person构造函数的调用" << endl;
}
};
void test01() {
Person p;
}
int main() {
test01();
system("pause");
return 0;
}
4、构造函数的两种分类
按参数分:有参构造和无参构造
按类型分:普通构造和拷贝构造
class Person {
public:
//构造函数 进行初始化操作
//无参构造(默认构造)
Person() {
cout << "Person无参构造函数的调用" << endl;
}
//有参构造
Person(int a) {
age = a;
cout << "Person有参构造函数的调用" << endl;
}
//拷贝构造函数:把Person类型的p对象具有的所有属性全部复制到Person函数中(根据自己理解写的)
Person(const Person &p) {
age = p.age;//将传入的对象身上所有属性拷贝到我身上
cout << "Person拷贝构造函数的调用" << endl;
}
//析构函数
~Person() {
cout << "Person的析构函数调用" << endl;
}
int age;
};
5、构造函数的三种调用方式
括号法
#include<iostream>
using namespace std;
class Person {
public:
//构造函数 进行初始化操作
//无参构造(默认构造)
Person() {
cout << "Person无参构造函数的调用" << endl;
}
//有参构造
Person(int a) {
age = a;
cout << "Person有参构造函数的调用" << endl;
}
//拷贝构造函数:把Person类型的p对象具有的所有属性全部复制到Person函数中(根据自己理解写的)
Person(const Person &p) {
age = p.age;//将传入的对象身上所有属性拷贝到我身上
cout << "Person拷贝构造函数的调用" << endl;
}
//析构函数
~Person() {
cout << "Person的析构函数调用" << endl;
}
int age;
};
void test01() {
//1.括号法
Person p1;//默认(无参)构造函数的调用
Person p2(10);//有参构造函数的调用
Person p3(p2);//拷贝构造函数的调用
cout << "p2的年龄:" << p2.age << endl;
cout << "p3的年龄:" << p3.age << endl;
/*注意,在调用默认(无参)构造函数时,不需要加括号,如果加了括号,
则不会显示默认构造函数里面的内容,因为Person p1()会被编译器认为是一个函数的声明*/
}
int main() {
test01();
system("pause");
return 0;
}
显示法
void test01() {
//2.显示法
Person p1;
Person p2 = Person(20);
Person p3 = Person(p2);
//注意1:匿名对象—当前执行结束后,系统会立即回收掉匿名对象
Person(10);
cout << "aaaaa" << endl;
//注意2:不要利用拷贝构造函数来初始化一个匿名对象,编译器会认为这是一个对象声明Person(p3)==Person p3
Person(p3);
}
隐式转换法
void test01() {
//1.括号法
/*Person p1;//默认(无参)构造函数的调用
Person p2(10);//有参构造函数的调用
Person p3(p2);//拷贝构造函数的调用
cout << "p2的年龄:" << p2.age << endl;
cout << "p3的年龄:" << p3.age << endl;*/
/*注意,在调用默认(无参)构造函数时,不需要加括号,如果加了括号,
则不会显示默认构造函数里面的内容,因为Person p1()会被编译器认为是一个函数的声明*/
//2.显示法
/*Person p1;
Person p2 = Person(20);
Person p3 = Person(p2);
//注意1:匿名对象—当前执行结束后,系统会立即回收掉匿名对象
Person(10);
cout << "aaaaa" << endl;
//注意2:不要利用拷贝构造函数来初始化一个匿名对象,编译器会认为这是一个对象声明Person(p3)==Person p3
//Person(p3);*/
//3.隐式转换法
Person p4 = 10;//相当于Person p4=Person(10)
Person p5 = p4;//拷贝构造
}
6、拷贝构造函数调用时机
拷贝构造函数调用时机通常有三种情况
(1)使用一个已经创建完毕的对象来初始化一个新对象
#include<iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Person无参构造函数的调用" << endl;
}
Person(int a) {
age = a;
cout << "Person有参构造函数的调用" << endl;
}
Person(const Person& p) {
age = p.age;
cout << "Person拷贝构造函数的调用" << endl;
}
~Person() {
cout << "Person的析构函数调用" << endl;
}
int age;
};
//(1)
void test01() {
Person p1(10);
Person p2(p1);
cout << "p2的年龄是:" << p2.age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
(2)值传递的方式给函数参数传值
#include<iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Person无参构造函数的调用" << endl;
}
Person(int a) {
age = a;
cout << "Person有参构造函数的调用" << endl;
}
Person(const Person& p) {
age = p.age;
cout << "Person拷贝构造函数的调用" << endl;
}
~Person() {
cout << "Person的析构函数调用" << endl;
}
int age;
};
//(2)
void dowork(Person p) {
}
void test02() {
Person p;//相当于调用默认构造函数
dowork(p);//调用拷贝构造函数
}
int main() {
test02();
system("pause");
return 0;
}
(3)值方式返回局部对象
//(3)
Person dowork2() {
Person p1;//调用默认构造函数
cout << (int*)&p1 << endl;
return p1;
}
void test03() {
Person p = dowork2();
cout << (int*)&p << endl;
}
int main() {
test03();
system("pause");
return 0;
}
7、构造函数调用规则
默认情况下,C++编译器至少会给类添加3个函数
(1)默认构造函数(无参,函数体为空)
(2)默认析构函数(无参,函数体为空)
(3)默认拷贝构造函数,对属性进行值拷贝
调用规则如下:
(1)如果用户自己定义了构造函数,C++就不会提供默认构造函数,但是会提供默认拷贝构造
(2)如果用户定义了拷贝构造,C++则不会再提供其他构造函数
8、深拷贝和浅拷贝(经典面试问题)
浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作
深拷贝示例:
#include<iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Person无参构造函数的调用" << endl;
}
Person(int a,int h) {
age = a;
height = new int(h);
cout << "Person有参构造函数的调用" << endl;
}
Person(const Person& p) {
cout << "Person拷贝构造函数调用" << endl;
age = p.age;
//深拷贝操作
height = new int(*p.height);
}
//析构函数,将堆区开辟数据做释放操作
~Person() {
if (height != NULL) {
delete height;
height = NULL;
}
cout << "Person的析构函数调用" << endl;
}
int age;
int* height;
};
void test() {
Person p1(10, 160);
cout << "p1的年龄是:" << p1.age << "身高为:" << *p1.height << endl;
Person p2(p1);
cout << "p2的年龄是:" << p2.age << "身高为:" << *p2.height << endl;
}
int main() {
test();
system("pause");
return 0;
}
如果属性有在 堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
三、析构函数
1、作用
析构函数主要作用于对象销毁前系统自动调用,执行一些清理工作
2、语法:~类名(){}
(1)没有返回值,也不写void(函数如果是void,则无返回值,如果有类型,则需要返回值)
(2)函数名称与类名相同,在名称前加上符号~
(3)析构函数不可以有参数,因此不可以发生重载
(4)程序在对象销毁前会自动调用析构,无需自己调用,而且只会调用一次
3、代码演示
#include<iostream>
using namespace std;
class Person {
public:
//构造函数 进行初始化操作
Person() {
cout << "Person构造函数的调用" << endl;
}
//析构函数
~Person() {
cout << "Person的析构函数调用" << endl;
}
};
void test01() {
Person p;//在栈上的数据,test01执行完毕后,释放这个对象
}
int main() {
test01();
system("pause");
return 0;
}
4、结果展示
四、区分
#include<iostream>
using namespace std;
class Person {
public:
//构造函数 进行初始化操作
Person() {
cout << "Person构造函数的调用" << endl;
}
//析构函数
~Person() {
cout << "Person的析构函数调用" << endl;
}
};
void test01() {
Person p;//在栈上的数据,test01执行完毕后,释放这个对象
}
int main() {
//test01();
Person p;//直接写在main函数中
system("pause");
return 0;
}
如果在main函数中没有调用test01函数,那么结果就不会显示析构函数的调用,原因是在test01调用完Person对象后,就会被释放,但如果在main函数中,这个对象不会被释放,往下执行的是system(“pause”),就会直接显示请按任意键继续。