Learning C++之 1.4c 关键字和命名标识符

本文介绍了C++语言中的84个关键字及其特殊意义,并详细解释了标识符的命名规则和建议,帮助初学者理解如何正确使用关键字及合理命名变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键字:

C++保留了84个关键字作为自己使用,这些保留字在C++中往往有特殊的意义。以下就是C++的关键字:

alignas **
alignof **
and
and_eq
asm
auto
bitand
bitor
bool *
break
case
catch
char
char16_t **
char32_t **
class
compl
const
constexpr **
const_cast *
continue
decltype
default
delete
do
double
dynamic_cast *
else
enum
explicit *
export *
extern
false *
float
for
friend
goto
if
inline
int
long
mutable *
namespace *
new
noexcept **
not
not_eq
nullptr **
operator
or
or_eq
private
protected
public
register
reinterpret_cast *
return
short
signed
sizeof
static
static_assert **
static_cast *
struct
switch
template
this
thread_local **
throw
true *
try
typedef
typeid *
typename *
union
unsigned
using *
virtual
void
volatile
wchar_t *
while
xor
xor_eq

*这些出自于C++98,**这些出自于C++11

C++11提出了两个特殊的关键字:override final。这两个关键字在某些场景下有特殊的意义。

之前的学习中已经遇到过很多关键字,比如int,char,retrurn等。这些关键字和一些运算符,标识符构成了整个的C++语言。因为这些字符有特殊的含义,所以当你编程的时候,IDE会特殊显示。

在你学完这些教程的时候,你会明白每一个关键字的用途。

标识符命名规则:

在C++中一些变量,函数,类型的名字叫做标识符。C++对标识符的要求是很宽松的,但是需要符合以下规范:

  • 标识符不能是关键字
  • 标识符只能是数字,字母和下划线。
  • 标识符只能以子母和下划线开头,不能以数字开头。
  • C++是区分大小写的语言,因此nValue和nvalue是不同的

既然你知道了怎样命名一个标识符,下面是一些命名变量和函数的建议:

首先,C++的变量都是以小写字母开头,这是一个习惯。如果变量只有一个单词,那么就全部用小写。

int value; // correct
 
int Value; // incorrect (should start with lower case letter)
int VALUE; // incorrect (should start with lower case letter)
int VaLuE; // incorrect (see your psychiatrist) ;)

通常来说,函数也应该以小写字母开头,为了保持一致性,我们也可以一这样的规则开头,因为main函数就是小写开始的。

以大写字母开始的标识符基本上是结构体,类或者枚举变量,后面我们会学到。

如果函数名或者变量以多个单词标识,那么有两种写法,一种是使用_区别开来,另一种是每个单词的首字母大写。

int my_variable_name; // correct (separated by underscores)
void my_function_name() // correct (separated by underscores)
 
int myVariableName; // correct (intercapped/CamelCase)
void myFunctionName(); // correct (intercapped/CamelCase)
 
int my variable name; // invalid (whitespace not allowed)
void my function name(); // invalid (whitespace not allowed) 
 
int MyVariableName; // valid but incorrect (should start with lower case letter)
void MyFunctionName(); // valid but not best practice

本教程中我们统一使用“驼峰式”写法,即第一个单词小写,之后的单词每个首字母大写。因为使用下划线在某种场景下容易和空格混淆。当然在C++的库中还是有很多使用下划线的情况的,我们之后也可以看到两种方式一起用的情况,这个都比较正常。

当然你如果在别人的代码中工作,那这些基本规则意义就不大了。普遍来讲你需要和工作中的代码格式一致,这样才有利于代码的维护。

其次尽量避免你自己的代码使用下划线开头,因为以下划线开头的话,一般是操作系统,库函数或者变异函数使用,容易造成混淆。

再次,最重要的规则是你一定要让你的命名清晰的表达它所含的意思。没有经验的程序员往往尽可能的简写他们的命名,要么是为了节省打字,要么就是感觉已经很明确了。这艘是一个错误的习惯。典型的来说,变量的命名应该可以让一个完全不懂你的代码的人立即明白该变量所表达的意思。一般三个月之后,你自己都不会明白自己的一些代码的意思,到时候你就会感谢你的详细地命名方式。所以,变量名越复杂,变量的名字也就定义的越好。如下:

int ccount	Bad	Nobody knows what a ccount is
int customerCount	Good	Clear what we’re counting
int i	Bad*	Generally bad unless use is trivial or temporary, such as loop variables
int index	Either	Okay if obvious what we’re indexing
int totalScore	Good	Descriptive
int _count	Bad	Do not start variable names with underscore
int count	Either	Okay if obvious what we’re counting
int data	Bad	What kind of data?
int value1, value2	Either	Can be hard to differentiate between the two
int numberOfApples	Good	Descriptive
int monstersKilled	Good	Descriptive
int x, y	Bad*	Generally bad unless use is trivial, such as in trivial mathematical functions

Note:当然对于一些琐碎的功能使用的琐碎的变量,如循环中计数变量还是越简单越好,用i就足够了。

当然明确地变量命名还有比较长的路要走。比如说我们定义了一个变量 numberOfChars想要保存字符到一个文件里面。那么“Hello World!”是包含10个还是11个还是12个字符?这区别于我们是否记录空格或者标点。比起定义文件名:numberOfCharsIncludingWhitespaceAndPunctuation 这么冗长的名字来说,在命名上加一行备注是比较好的方式。

// holds number of chars in a piece of text -- including whitespace and punctuation!
int numberOfChars;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值