当前位置:网站首页>构造函数和析构函数
构造函数和析构函数
2022-06-26 16:29:00 【Meaauf】
构造函数
C++的目的是为了让使用类像使用标准类型一样,但是上述并不能很好的使用类对象
struct Student()
{
string name;
int age;
};
Student s={
"Mike",19}; // 使用结构体可以很方便的初始化值
在结构体中,可以非常方便的初始化结构体成员,而类不行,因为类的数据部分的访问状态是私有的,这意味着程序不能直接访问数据成员;C++为我们提供了构造函数,方便我们在创建对象时对它进行初始化。
class Student
{
public:
Student // 定义构造函数 与类名一致
{
cout << "调用了构造函数" << endl;
}
}
构造函数的初衷是为了给类成员赋予初始值;因此我们可以在构造函数中将参数赋予给类成员,在定义类成员名的时候,参数名应不能与类成员名相同,常见的做法是在数据成员名中使用 m_ 前缀或在成员名中使用后缀 _
class Student
{
private:
string m_name;
public:
Student(string name) // 传递参数
{
m_name=name; // 见参数值传递给类成员
cout << "调用了构造函数--" << m_name << endl;
}
}
C++提供了两种使用构造函数来初始化对象的方式,显示调用和隐式调用;两种调用方式都是等价的,其中隐式调用更加紧凑简洁
Student s1=Student("Mike"); // 显示调用
Student s2("John"); // 隐式调用
构造函数的使用方式不同于其他类方法,我们可以通过对象来调用方法,但是不能通过对象来调用构造函数,在构造函数建立对象之前,对象是不存在。也就是说:构造函数是有来创建对象而不能通过对象来调用。
默认构造函数
默认构造函数是在未提供显示初始值的时候,用来创建对象的构造函数;如果类中没有定义任何构造函数,编译器会提供默认的构造函数;如果代码编写了构造函数,编译器则会使用用户编写的构造函数
// 如果编写了构造函数 但没有提供参数
public:
Student(string name)
{
cout << name << endl;
}
Student s; // [Error] no matching function for call to 'Student::Student()'
如果要定义默认构造函数,我们通过给构造函数参数提供默认初始值或者使用函数重载;因为只能有一个默认构造函数,所以尽量不要同时使用两种方式。在初始化所有对象的时候,就应该确保所有成员都存在一个已知且合理的值
// 方法1: 提供默认值
public:
Student(string name="Mike")
{
cout << name << endl;
}
// 方法2: 函数重载
Student()
{
;
}
在使用创建无参构造函数的时候应该注意,在实例化对象的时候避免使用括号创建对象
Student()
{
cout << "调用了构造函数" << endl;
}
// 实例化对象
Student s; // "调用了构造函数"
Student s(); // 无输出值
但使用Student s()时,编译器会认为你是在main函数中声明了一个函数,返回值为Student的函数
析构函数
在构造函数创建对象后,在程序过期时,会自动调用析构函数。在实际使用中,如果构造函数使用new分配了内存,则析构函数则可以使用delete释放内存。与构造函数一样,析构函数可以没有返回值和声明类型;不同的是:析构函数没有参数。
// 定义一个基础析构函数
class Student
{
public:
~Student()
{
cout << "Bye." << endl;
}
}
编译器会决定什么时候调用析构函数
- 如果创建的是静态存储类对象,析构函数会在程序结束时自动被调用;
- 如果创建的是自动存储类对象,析构函数会在程序执行完代码块后自动调用;
- 如果使用new创建,则会存留在栈中,当使用delete释放内存后, 析构函数会自动调用
在类对象过期后,析构函数会自动调用,因此必须存在一个析构函数,如果程序并未编写析构函数,则编译器会隐式声明一个默认的析构函数,在对象被删除的后,提供默认的析构函数
匿名对象
匿名对象的特点是:当前语句执行完毕后,系统会立即销毁匿名对象
class Student // 定义类
{
public:
Student()
{
cout << "调用了构造函数" << endl;
}
~Student()
{
cout << "调用了析构函数" << endl;
}
};
Student(); // 创建匿名对象
cout << "Bye." << endl;
普通对象
匿名对象
边栏推荐
- 【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
- Net基于girdview控件实现删除与编辑行数据
- JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
- 100+ data science interview questions and answers Summary - basic knowledge and data analysis
- R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
- C# 读写文件从用户态切到内核态,到底是个什么流程?
- (1) Keras handwritten numeral recognition and recognition of self written numbers
- 10 tf. data
- 01 backpack DP
- Cloud platform monitoring system based on stm32+ Huawei cloud IOT design
猜你喜欢
Handwritten numeral recognition, run your own picture with the saved model
IAR engineering adapts gd32 chip
Simple use of tensor
精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增
Net基于girdview控件实现删除与编辑行数据
1-12vmware adds SSH function
I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!
Arduino UNO + DS1302简单获取时间并串口打印
C# 读写文件从用户态切到内核态,到底是个什么流程?
知道这几个命令让你掌握Shell自带工具
随机推荐
Arduino uno + DS1302 simple time acquisition and serial port printing
Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
Mono 的一些实例方法
Codeforces Round #802 (Div. 2)
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
6 custom layer
【力扣刷题】单调栈:84. 柱状图中最大的矩形
"C language" question set of ⑩
(1) Keras handwritten numeral recognition and recognition of self written numbers
Kept to implement redis autofailover (redisha)
Redis order sorting command
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
若依如何实现接口限流?
day10每日3题(1):逐步求和得到正数的最小值
The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
[207] several possible causes of Apache crash
Hyperf框架使用阿里云OSS上传失败
Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit