关键词
c++有63个关键字,这63个关键字在c++中都有着特殊的含义。加星的关键字是初始版本没有但是后来加进来的。所以老一些的书籍和材料会没有这些。
下面就是关键词的列表:
asm auto bool * break case catch char class | const const_cast * continue default delete do double dynamic_cast * | else enum explicit * export * extern false * float for | friend goto if inline int long mutable * namespace * | new operator private protected public register reinterpret_cast * return | short signed sizeof static static_cast * struct switch template | this throw true * try typedef typeid * typename * union | unsigned using * virtual void volatile wchar_t * while |
一些词可能你已经遇到了,比如int,void ,return ,using,和namespace。这些关键字加上操作符就构成了整个的c++语言(预处理不包括在内)。因为这些关键字有着特殊的含义,所以在IDE中,遇到这些关键字的时候,文本会有渲染效果,比如是蓝色,红色等。
在你学完了整个教程之后这里的几乎所有的关键字你应该都会掌握如何使用了。
标识符和命名
变量,函数,类在c++中的名字都是标识符。c++给了用户很大的命名自由。但是也有一些必须要遵守的规则。
- 名字不能和c++本身的关键字冲突
- 名字中只能是字母数字下划线。不可以出现其他的符号或者空格。
- 名字的必须以字母或者是下划线进行起始。不能用数字进行开头。
- . 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) ;)
|
如果名字中包含多个单词,那么一般有两个常用习惯需要遵守。两个单词可以用下划线分开,也可以用大小写来区分。
int
my_variable_name;
// correct (separated by underscores)
int
myVariableName;
// correct (intercapped)
int
my variable name;
// incorrect (spaces not allowed)
int
MyVariableName;
// incorrect (should start with lower case letter)
|
因为大小写更好读,所以本教程中使用大小写来区分多单词的变量名。
第二:这条也是最重要的一条。你的名字应该是描述他们是什么。一些初学者经常起名字的时候本着越短越好的心理,好打省事。他们认为变量名字的含义是显而易见的。这样的命名习惯是非常不好的。因为这些名字就应该让阅读这段代码的人迅速了解这些变量分别的含义是什么。这些代码在几个月后也许你自己也不能辨识出他们到底是什么。越复杂的代码,命名的时候就越需要考究。
int ccount | Bad | Nobody knows what a ccount is |
int customerCount | Good | Clear what we’re counting |
int i | Bad | What does I stand for?* |
int index | Good | This variable is indexing something |
int _count | Bad | Do not start variable names with underscore |
int count | Either | Okay only 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 totalScore | Good | Descriptive |
int monstersKilled | Good | Descriptive |
int x, y | Either | Okay only in trivial mathematical functions |
*此处注意:一些平常的变量也可以取一些平常的名字,比如那些for循环里用来计数的变量。我们将会在控制流一章中继续谈这些问题。
第三:一段说明的注释也是非常有用的。举个例子,我们声明一个叫做numberOfChars的变量,它用来记录文本中字母的数量。那么“Hello World!”到底有10,11还是12个字母呢?这取决我们是否包括空格和标点。我们总不能取一个numberOfCharsIncludingWhitespaceAndPunctuation 的变量名把。所以这时候就需要注释来解决这个问题。
|
匈牙利标志法
匈牙利标志法是经常用来命名变量的一种方法。在这个方法中,变量要带上一个说明他们类型的前缀。比如
|
|
我们在涉及到多种数字类型的时候自然会深入讲匈牙利标志法。
小测验
根据规则哪个声明是不符合的:
1) int sum;
2) int _apples;
3) int cats=5, dogs=5;
4) int VALUE;
5) int clouds=5, int trees;
6) int my variable name;
7) int length=3; int width=4;
8) int TotalCustomers;
9) int void = 5;
10) int nAngle;
11) int 3some;
12) int meters_of_pipe;
13) int length, width=5;
4) Variable names should start with a lower case letter.
5) Compiler will complain about the “int” before trees.
6) Variable names can not contain spaces.
8) Variable names should start with a lower case letter.
9) Void is a keyword.
11) Variable names can not start with a number.
13) Length is uninitialized.