当前位置:网站首页>构造函数和析构函数
构造函数和析构函数
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;
普通对象

匿名对象

边栏推荐
- 牛客小白月赛50
- 我把它当副业月入3万多,新手月入过万的干货分享!
- [force deduction question] two point search: 4 Find the median of two positive arrays
- [from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
- Mono 的一些实例方法
- [从零开始学习FPGA编程-46]:视野篇 - 集成电路的发展与技术进步
- Stm32h7b0 replaces the h750 program, causing the MCU to hang up and unable to burn the program
- Which position does Anxin securities rank? Is it safe to open an account?
- R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
- Some instance methods of mono
猜你喜欢

stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题

IAR工程适配GD32芯片

架构实战营毕业设计

In a bad mood, I just write code like this

神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读

100+数据科学面试问题和答案总结 - 基础知识和数据分析
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock

精致妆容成露营“软实力”,唯品会户外美妆护肤产品销量激增

2 three modeling methods

【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
随机推荐
理想路径问题
Arduino uno + DS1302 simple time acquisition and serial port printing
Kept to implement redis autofailover (redisha)
IAR engineering adapts gd32 chip
6 custom layer
[force deduction question] two point search: 4 Find the median of two positive arrays
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
Learn about common functional interfaces
Keepalived 实现 Redis AutoFailover (RedisHA)1
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!
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
若依微服务特殊字符串被过滤的解决办法
今年高考英语AI得分134,复旦武大校友这项研究有点意思
Cuckoo filter for Chang'an chain transaction
11 introduction to CNN
R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
Practice of federal learning in Tencent micro vision advertising
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
Redis 迁移(操作流程建议)
Solution for filtering by special string of microservice