构造函数和析构函数
- 在对象创建时,需要初始化一些内容—使用构造函数
- 在使用完毕后,需要进行一些清理工作—使用析构函数
- 构造函数和析构函数由编译器自动调用,不管写不写,编译器都会调用
- 如果自己不定义,编译器会自动调用自己内部的构造和析构函数,是空实现
析构函数
- ~类名(){}
- 没有返回值,也不用写void
- 函数名称与类名相同,前面加~
- 析构函数不能有参数
- 在对象销毁前会自动调用,且只调用一次
code:
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "自定义构造函数被调用" << endl;
}
~Person()
{
cout << "自定义析构函数被调用" << endl;
}
};
void test()
{
Person p1;
cout << "这是个测试项" << endl;
}
void main()
{
test();
system("pause");
}
result:
自定义构造函数被调用
这是个测试项
自定义析构函数被调用
构造函数
构造函数的语法
- 类名(){}
- 没有返回值,也不用写void
- 函数名称与类名相同
- 构造函数可以有参数,可以发生重载
- 创建对象时会自动调用,而且只调用一次
构造函数的分类
- 按照参数分
– 有参和无参构造函数 - 按照类型分
– 普通和拷贝构造函数
拷贝构造函数
- 定义拷贝构造函数,Person(const Person& p),里面可以使用p上的属性给要实例化的对象赋值
构造函数的调用方式
- 括号法----推荐
– 当构造函数无参时,实例化对象使用Person p1;
– 当调用有参构造函数时,实例化对象使用Person p2(10);
– 当调用拷贝构造函数时,实例化对象使用Person p2 (p1); - 显式法
– 当构造函数无参时,实例化对象时使用Person p1;
– 当调用有参构造函数时,实例化对象使用 Person p2 = Person(18);
– 当调用拷贝构造函数时,实例化对象使用 Person p3 = Person(p2);
– Person(18):本身是匿名对象,本来没名,赋值给左方,当前行执行完,就会释放
– 不要使用拷贝构造函数初始化匿名对象,如单独的一行Person§; 编译器会认为是person p; 认为是在实例化对象p,重定义错误 - 隐式法
– 当调用有参构造函数时,实例化对象使用 Person p2 = 18; 编译器会自动转换为显示法
– 当调用拷贝构造函数时,实例化对象使用 Person p3 = p2;
注意事项
- 无参构造函数调用不能加括号,Person p1();编译器会认为是函数的声明,认为Person是返回类型
- Person(18) 匿名对象,当前行执行完,会立马释放
- 不要利用拷贝构造函数初始化匿名对象,Person(p3),编译器会认为是Person p3, 发生了重定义
#include<iostream>
using namespace std;
#include"circle.h"
class Person
{
private:
int m_age = 10;
public:
Person()
{
cout << "无参构造参数被调用" << endl;
}
Person(int age)
{
m_age = age;
cout << "有参构造函数被调用," << "年龄是" << m_age << endl;
}
Person(const Person& p)
{
m_age = p.m_age;
cout << "拷贝构造函数被调用, " << "年龄是" << m_age << endl;
}
~Person()
{
cout << "析构函数被调用," << "年龄是" << m_age << endl;
}
};
void test1()
{
Person p1;
Person p2(18);
Person p3(p2);
}
void test2()
{
Person p1;
Person p2 = Person(18);
Person p3 = Person(p2);
Person(26);
}
void test3()
{
Person p1;
Person p2 = 66;
Person p3 = p2;
}
void main()
{
test1();
cout << endl;
test2();
cout << endl;
test3();
system("pause");
}
result:
无参构造参数被调用
有参构造函数被调用,年龄是18
拷贝构造函数被调用, 年龄是18
析构函数被调用,年龄是18
析构函数被调用,年龄是18
析构函数被调用,年龄是10
无参构造参数被调用
有参构造函数被调用,年龄是18
拷贝构造函数被调用, 年龄是18
有参构造函数被调用,年龄是26
析构函数被调用,年龄是26
析构函数被调用,年龄是18
析构函数被调用,年龄是18
析构函数被调用,年龄是10
无参构造参数被调用
有参构造函数被调用,年龄是66
拷贝构造函数被调用, 年龄是66
析构函数被调用,年龄是66
析构函数被调用,年龄是66
析构函数被调用,年龄是10
请按任意键继续. . .
拷贝构造函数的调用时机
- 利用存在的对象初始化新对象
- 值传递的方式传参
- 值方式返回局部对象
**#include<iostream>
using namespace std;
#include"circle.h"
class Person
{
public:
int age = 10;
Person()
{
cout << "无参构造参数被调用" << endl;
}
Person(int ref_age)
{
age = ref_age;
cout << "有参构造函数被调用," << "年龄是" << age << endl;
}
Person(const Person& ref_p)
{
age = ref_p.age;
cout << "拷贝构造函数被调用, " << "年龄是" << age << endl;
}
~Person()
{
cout << "析构函数被调用," << "年龄是" << age << endl;
}
};
void check_ref(Person ref_p)
{
}
Person check_return()
{
Person p1(33);
cout << &p1 << endl;
return p1;
}
void test1()
{
Person p1(11);
Person p2(p1);
}
void test2()
{
Person p1(22);
check_ref(p1);
}
void test3()
{
Person p1;
cout << &p1 << endl;
p1 = check_return();
}
void main()
{
test1();
cout << endl;
test2();
cout << endl;
test3();
system("pause");
}**
构造函数的调用规则
- 创建一个对象,C++编译器会给每个类至少添加3个函数
– 默认构造函数(无参),空函数
– 拷贝构造函数(值拷贝)
– 析构函数,空函数 - 如果自定义了有参构造函数,编译器就不再提供默认构造函数,但是仍然提供拷贝构造函数。
- 如果自定义了拷贝构造函数,变异就不再提供其它的构造函数。