目录
4.2.1:如果我们写了有参构造函数,C++不再提供默认无参构造,但是依然会提供默认拷贝构造
4.2.2:如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数了
- 生活中我们买的电子产品否基本会出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
- c++ 中的面向对象来源于生活,每个对象也都会又初始设置以及对象销毁前的清理数据的设置
1、构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始化状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时的清理,也会造成一定的安全问题
C++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象的初始化和清理工作。对象的初始化和清理工作是编译器强制我们要做的事情,因此如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现。
- 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
- 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
构造函数语法: 类名( ){ }
- 构造函数,没有返回值也不写void
- 函数名称和类名相同
- 构造函数可以有参数,因此可以发生重载
- 程序在调用对象时候会自动调用构造,无需手动调用,而且只会调用一次
析构函数语法:~类名( ){ }
- 析构函数,没有返回值也不写void
- 函数名称与类名相同,在名称前加上符号 ~
- 析构函数不可以有参数,因此不可以发生重载
- 程序在对象销毁前会自动调用析构,无需手动调用,而且只会调用一次
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "这是构造函数的调用" << endl;
}
~Person()
{
cout << "这是析构函数的调用" << endl;
}
};
//构造和析构都是必须有的实现,如果我们自己不提供,编译器就会提供一个空实现的构造和析构
void test()
{
Person p; //在栈上的数据,test()执行完毕之后,释放这个对象
}
int main()
{
test();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "这是构造函数的调用" << endl;
}
~Person()
{
cout << "这是析构函数的调用" << endl;
}
};
//构造和析构都是必须有的实现,如果我们自己不提供,编译器就会提供一个空实现的构造和析构
/*
void test()
{
Person p; //在栈上的数据,test()执行完毕之后,释放这个对象
}
*/
int main()
{
//test();
Person p;
system("pause");
return 0;
}
可以看出在对象销毁前调用析构函数
2、构造函数的分类及调用
两种分类方式:
- 按参数分为:有参构造和无参构造
- 按类型分为:普通构造和拷贝构造
三种调用方式:
- 括号法
- 显示法
- 隐式转换法
2.1、括号法
#include <iostream>
#include <cstdio>
using namespace std;
//1构造函数的分类及调用
//分类
// 按照参数分类 无参构造(默认构造函数) 和 有参构造
// 按照类型分类 普通构造 拷贝构造
class Person
{
public:
//构造函数
Person()
{
cout << "Person的无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
//将传入的人身上的所有属性,拷贝到我身上
cout << "Person的拷贝构造函数调用" << endl;
age = p.age;
}
//析构函数
~Person()
{
cout << "Person的析构调用" << endl;
}
int age;
};
//调用
void test01()
{
//1、括号法
Person p1; //默认构造函数调用
Person p2(10); //有参构造函数
Person p3(p2); //拷贝构造函数
//注意事项
//调用默认构造函数时候,不要加 ( )
//因为下面这行代码,编译器会认为是一个函数的声明,如void func(); 不会认为是在创建对象
//Person p1();
cout << "p2的年龄为:" << p2.age <<endl;
cout << "p3的年龄为:" << p3.age <<endl;
}
int main()
{
test01();
return 0;
}
2.2、显示法
#include <iostream>
#include <cstdio>
using namespace std;
//1构造函数的分类及调用
//分类
// 按照参数分类 无参构造(默认构造函数) 和 有参构造
// 按照类型分类 普通构造 拷贝构造
class Person
{
public:
//构造函数
Person()
{
cout << "Person的无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
//将传入的人身上的所有属性,拷贝到我身上
cout << "Person的拷贝构造函数调用" << endl;
age = p.age;
}
//析构函数
~Person()
{
cout << "Person的析构调用" << endl;
}
int age;
};
//调用
void test01()
{
//2、显示法
Person p1; //默认构造函数的调用
Person p2 = Person(10); //有参构造
Person p3 = Person(p2); //拷贝构造
//Person(10); //匿名对象(创建了一个对象,但是没有名字) 特点:当前行执行结束后,系统会立即回收掉匿名对象
//cout << "bbbbb" << endl;
//注意事项2
//不要利用拷贝构造函数,初始化匿名对象 编译器会认为 Person(p3) === Person p3; 对象声明
//Person(p3); //重定义了一个p3对象,前面已经定义了一个p3对象
}
int main()
{
test01();
return 0;
}
2.3、隐式转换法
#include <iostream>
#include <cstdio>
using namespace std;
//1构造函数的分类及调用
//分类
// 按照参数分类 无参构造(默认构造函数) 和 有参构造
// 按照类型分类 普通构造 拷贝构造
class Person
{
public:
//构造函数
Person()
{
cout << "Person的无参构造函数调用" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
//将传入的人身上的所有属性,拷贝到我身上
cout << "Person的拷贝构造函数调用" << endl;
age = p.age;
}
//析构函数
~Person()
{
cout << "Person的析构调用" << endl;
}
int age;
};
//调用
void test01()
{
//3、隐式转换法
Person p4 = 10; //相当于 写了 Person p4 = Person(10); 有参构造
Person p5 = p4; //拷贝构造
}
int main()
{
test01();
return 0;
}
3、拷贝构造函数的调用时机
c++中拷贝构造函数调用时机通常有三种情况
- 使用一个已经创建完毕的对象来初始化一个新对象
- 值传递的方式给函数参数传参
- 以值方式返回局部对象
3.1 使用一个已经创建完毕的对象来初始化一个新对象
#include <iostream>
using namespace std;
//拷贝构造函数调用时机
class Person {
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person &p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数的调用" << endl;
}
int m_Age;
};
//1、使用一个已经创建完毕的对象来初始化一个新对象
void test01()
{
Person p1(20);
Person p2(p1);
cout << "P2的年龄为:"<< p2.m_Age <<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
3.2 值传递的方式给函数参数传值
#include <iostream>
using namespace std;
//拷贝构造函数调用时机
class Person {
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person &p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数的调用" << endl;
}
int m_Age;
};
//2、值传递的方式给函数参数传值
void doWork(Person p)
{
}
void test02()
{
Person p;
doWork(p);
}
int main()
{
test02();
system("pause");
return 0;
}
3.3 值方式返回局部对象
#include <iostream>
using namespace std;
//拷贝构造函数调用时机
class Person {
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person &p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person析构函数的调用" << endl;
}
int m_Age;
};
//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;
}
4、构造函数调用规则
1、默认情况下,C++编译器至少给一个类添加3个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
2、构造函数调用规则如下:
- 如果我们写了有参构造函数,C++不再提供默认无参构造,但是依然会提供默认拷贝构造
- 如果用户定义拷贝构造函数,C++不会再提供其他构造函数
第2点总结:
按顺序: 默认无参构造函数-->有参构造函数-->拷贝构造函数。
如果我们写了有参构造函数,后面的拷贝构造编译器会提供,但前面的默认构造函数则不提供
如果我们写了拷贝构造函数,前面的两个都不提供
4.1:默认情况下,C++编译器至少给一个类添加3个函数
自己写了拷贝构造函数:
#include <iostream>
using namespace std;
//构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造 (空实现)
//析构函数 (空实现)
//拷贝构造 (值拷贝)
class Person
{
public:
Person()
{
cout << "Person的默认构造函数" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数" << endl;
m_Age = age;
}
Person(const Person &p)
{
cout << "Person的拷贝构造函数" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "这是析构函数" << endl;
}
int m_Age;
};
void test01()
{
Person p;
p.m_Age = 18;
Person p2(p);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造 (空实现)
//析构函数 (空实现)
//拷贝构造 (值拷贝)
class Person
{
public:
Person()
{
cout << "Person的默认构造函数" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数" << endl;
m_Age = age;
}
//Person(const Person &p) //自己写的拷贝构造函数
//{
// cout << "Person的拷贝构造函数" << endl;
// m_Age = p.m_Age;
//}
~Person()
{
cout << "这是析构函数" << endl;
}
int m_Age;
};
void test01()
{
Person p;
p.m_Age = 18;
Person p2(p);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
4.2.1:如果我们写了有参构造函数,C++不再提供默认无参构造,但是依然会提供默认拷贝构造
#include <iostream>
using namespace std;
//2、如果我们写了有参构造函数,编译器就不会再提供默认构造,依然提供拷贝构造
class Person
{
public:
//Person()
//{
// cout << "Person的默认构造函数" << endl;
//}
Person(int age)
{
cout << "Person的有参构造函数" << endl;
m_Age = age;
}
~Person()
{
cout << "这是析构函数" << endl;
}
int m_Age;
};
void test02()
{
Person p;
}
int main()
{
test02();
system("pause");
return 0;
}
4.2.2:如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数了
#include <iostream>
using namespace std;
//2、如果我们写了有参构造函数,编译器就不会再提供默认构造,依然提供拷贝构造
class Person
{
public:
//Person()
//{
// cout << "Person的默认构造函数" << endl;
//}
//Person(int age)
//{
// cout << "Person的有参构造函数" << endl;
// m_Age = age;
//}
Person(const Person &p)
{
cout << "Person的拷贝构造函数" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "这是析构函数" << endl;
}
int m_Age;
};
void test02()
{
Person p;
}
int main()
{
test02();
system("pause");
return 0;
}
5、深拷贝与浅拷贝
深拷贝是面试经典问题,也是常见的一个坑
浅拷贝:简单的复制拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作
5.1:浅拷贝
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int m_Age; //年龄
};
void test01()
{
Person p1(18);
cout << "p1的年龄是:"<< p1.m_Age << endl;
Person p2(p1);
cout << "p2的年龄是:" << p2.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5.2:浅拷贝存在的问题
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age,int height)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
m_Height = new int(height);
}
~Person()
{
//析构代码,将堆区开辟的数据做释放操作
if (m_Height != NULL){
delete m_Height;
m_Height = NULL; //防止野指针的出现
}
cout << "Person的析构函数调用" << endl;
}
int m_Age; //年龄
int *m_Height; //身高
};
void test01()
{
Person p1(18,160);
cout << "p1的年龄是:"<< p1.m_Age << "p1的身高是:" << *p1.m_Height <<endl;
Person p2(p1);
cout << "p2的年龄是:" << p2.m_Age << "p2的身高是:" << *p2.m_Height << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
为什么会出现这个错误呢?
那是因为浅拷贝遗留的问题,p1浅拷贝和p2深拷贝访问了同一个地址空间。
栈空间:先进后出
所以 p2先进行析构,此时已将 m_Height 的空间delete释放了
然后到p1进行析构的时候,就访问不到 m_Height 的空间 ,为非法操作
5.3:浅拷贝的问题,要利用深拷贝来解决
5.3.1:自己实现一个拷贝构造函数来解决浅拷贝带来的问题
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age, int height)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
m_Height = new int(height);
}
//自己实现拷贝构造函数,来解决浅拷贝带来的问题
Person(const Person &p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
//m_Height = p.m_Height; //编译器默认实现就是这个代码
m_Height = new int(*p.m_Height);
}
~Person()
{
if (m_Height != NULL){
delete m_Height;
m_Height = NULL;
}
cout << "Person的析构函数调用" << endl;
}
int m_Age; //年龄
int *m_Height; //身高
};
void test01()
{
Person p1(18,160);
cout << "p1的年龄是:"<< p1.m_Age << " p1的身高是:" << *p1.m_Height <<endl;
Person p2(p1);
cout << "p2的年龄是:" << p2.m_Age << " p2的身高是:" << *p2.m_Height << endl;
}
int main()
{
test01();
system("pause");
return 0;
}