刺猬在天冷时围抱取暖,但保持一定距离避免|

jinganglang567

园龄:4年5个月粉丝:0关注:20

📂c++
🔖c++
2023-02-12 11:01阅读: 50评论: 0推荐: 0

构造函数与析构函数

构造函数和析构函数

构造函数:在创建对象时,自动的进行初始化工作。

析构函数:在销毁对象前,自动的完成清理工作。

1)构造函数

语法:类名(){......}

l 访问权限必须是public。

l 函数名必须与类名相同。

l 没有返回值,不写void。

l 可以有参数,可以重载,可以有默认参数。

l 创建对象时只会自动调用一次,不能手工调用。

2)析构函数

语法:~类名(){......}

l 访问权限必须是public。

l 函数名必须在类名前加~。

l 没有返回值,也不写void。

l 没有参数,不能重载。

l 销毁对象前只会自动调用一次,但是可以手工调用。

注意:

1) 如果没有提供构造/析构函数,编译器将提供空实现的构造/析构函数。

2) 如果提供了构造/析构函数,编译器将不提供空实现的构造/析构函数。

3) 创建对象的时候,如果重载了构造函数,编译器根据实参匹配相应的构造函数。没有参数的构造函数也叫默认构造函数。

4) 创建对象的时候不要在对象名后面加空的圆括号,编译器误认为是声明函数。(如果没有构造函数、构造函数没有参数、构造函数的参数都有默认参数)

5) 在构造函数名后面加括号和参数不是调用构造函数,是创建匿名对象。

6) 接受一个参数的构造函数允许使用赋值语法将对象初始化为一个值(可能会导致问题,不推荐)。

7) 用new/delete创建/销毁对象时,也会调用构造/析构函数。

8) 不建议在构造/析构函数中写太多的代码,可以调用成员函数。

9) 除了初始化,不建议让构造函数做太多工作(只能成功不会失败)。

10) C++11支持使用统一初始化列表。

12) 如果类的成员也是类,创建对象的时候,先构造成员类;销毁对象的时候,先析构成员类。

本文作者:jinganglang567

本文链接:https://www.cnblogs.com/tgfoven/p/17113432.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   jinganglang567  阅读(50)  评论(0编辑  收藏  举报
  1. 1 look after you louis tomlinson
  2. 2 just hold on louis tomlinson
  3. 3 i can steven cooper
  4. 4 rock me one direction
  5. 5 just can't let her go one direction
  6. 6 this town Niall Horan
  7. 7 cut in love july
  8. 8 nemo 夜愿
  9. 9 in the end Black Veil Brides
  10. 10 glad you came the wanted
  11. 11 chasing the sun the wanted
  12. 12 TAKE MY HAND simple plan
  13. 13 wish i had an angel 夜愿
just hold on - louis tomlinson
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Sasha Alex Sloan/Eric Rosse

作曲 : Steve Aoki/Louis Tomlinson/Sasha Alex Sloan/Nolan Lambroza/Eric Rosse

Wish that you could build a time machine

So you could see

The things no one can see

Feels like you're standing on the edge

Looking at the stars

And wishing you were them

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

It's not over until it's all been said

It's not over until your dying breath

So what do you want them to say when you're gone?

That you gave up or that you kept going on?

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

Oooh

If it all goes wrong

Oooh

Darling just hold on

Oooh

If it all goes wrong

Darling just hold on

点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起