Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
200
5
码中(从硬件视角来看),指针和引用在本质上是同一事物。在此层次,有一句老话
说得对:引用不过是会提领的指针罢了。既然如此,
MinVals
按引用传递和按指针传
递结果也就没有什么区别了。基于同样的理由,也得准备某块内存以供指针去指涉。
按引用传递整型
static const
员变量通常要求其加以定义,而这个需求就会导致
代码完美转发失败而等价的、未使用完美转发的代码却能成功。
你可能已经注意到,在先前的讨论中我在有些地方闪烁其词。代码“应该无法”链接,
引用“通常”是当指针处理的,按引用传递整型
static const
成员
变量通常要求其
加以定义。仿佛我知道一些事情,但是不是很想一吐为快。
好吧,确有其事。依据标准,按引用传递
MinVals
时要求
MinVals
有定义,但并不
是所有实现都服从了这个需求。因此,你可能会发现有时是能够完美转发未加定义的
static const
成员变量的,这取决于具体的编译器和链接器。如果真是这样,恭喜,
不过没有理由期望这样的代码能够移植。若想添加可移植性,只需
static const
成员
变量提供定义即可。对于
MinVals
,定义如下:
const std::size_t Widget::MinVals;
//
Widget
.cpp
文件中
注意,定义语句没有重复指定初始化物(对于本例中的
MinVals
,就是值
28
),不过,
该细节不用死记硬背。如果忘记了这一点,在两处都提供了初始化物,你的编译器肯
定会发出控诉,从而提醒只在一处指定即可。
译注
3
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

C++程序设计:原理与实践(基础篇)(原书第2版)

C++程序设计:原理与实践(基础篇)(原书第2版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

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

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749