Skip to Content
C++语言导学(原书第2版)
book

C++语言导学(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

1.5 作用域和生命周期

声明语句将一个名字引入到一个作用域中:

·局部作用域(local scope):声明在函数(参见1.3节)或者lambda(参见6.3.2节)内的名字称为局部名字(local name)。局部名字的作用域从声明它的地方开始,到声明语句所在的块的末尾为止。块(block)用花括号{}限定边界。函数参数的名字也属于局部名字。

·类作用域(class scope):如果一个名字定义在一个类(参见2.2节、2.3节和第4章)中,且位于任何函数(参见1.3节)、lambda(参见6.3.2节)或enum class(参见2.5节)之外,则称之为成员名字(member name),或类成员名字(class member name)。成员名字的作用域从包含它的声明的起始{开始,到该声明结束为止。

·名字空间作用域(namespace scope):如果一个名字定义在一个名字空间(参见3.4节)内,同时位于任何函数、lambda(参见6.3.2节)、类(参见2.2节、2.3节和第4章)或enum class(参见2.5节)之外,则称之为名字空间成员名字(namespace member name)。它的作用域从其声明位置开始,到名字空间结束为止。

声明在所有结构之外的名字称为全局名字(global name),我们称其位于全局名字空间(global namespace)中。

此外,对象也可以没有名字,比如临时对象或者用new(参见4.2.2节)创建的对象。例如:

我们必须先构造(初始化)对象,然后才能使用它,对象在作用域的末尾被销毁。对于名字空间对象来说,它的销毁点在整个程序的末尾。对于成员来说,它的销毁点依赖于它所属对象的销毁点。用new创建的对象一直“存活”到delete(参见4.2.2节)销毁了它为止。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280