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++(中文版)
转向现代
C++
61
条款
8
:优先选用
nullptr
,而非
0
NULL
开门见山摆事实吧:字面常量
0
的型别是
int
,而非指针。当
C++
在只能使用指针的
语境中发现了一个
0
,它也会把它勉强解释为空指针,但说到底这是一个不得已而为
之的行为。
C++
的基本观点还是
0
的型别是
int
而非指针。
从实际效果来说,以上结论对于
NULL
也成立。虽然说
NULL
在技术细节上有一些不清
不楚的成分,因为标准允许各个实现给予
NULL
int
整型型别(如
long
)。固然
这样的实现不多,但其实也不重要,这里的关键并不在于
NULL
的精确型别,而在于
0
NULL
都不具备指针型别。
C++98
中,这样的基本观点可能在指针型别和整型之间进行重载时可能会发生意外。
如果向这样的重载函数传递
0
NULL
,是
从来不会调用到要求指针型别的重载版本的。
void f(int);
// f
的三个重载版本
void f(bool);
void f(void*);
f(0);
//
调用的是
f(int)
,而不是
f(void*)
f(NULL);
//
可能通不过编译,但一般会调用
f(int)
。从来不会调用
f(void*)
f(NULL)
的不确定性是
NULL
的型别在实现中的余地的一种反映。打个比方,假设
NULL
的定义为
0L
(即
long
型别中的
0
),那么这个调用就有多义性了。因为从
long
int
、从
long
bool
,还有从
0L
void*
的型别转换是被视为同样好的。这个调用有
意思的地方在于源代码想要表达的本来意思(“我在调用
f
时传入了 ...
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