2.2关键词和命名规则

关键词

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   的变量名把。所以这时候就需要注释来解决这个问题。

 

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

int numberOfChars;

 

 匈牙利标志法

匈牙利标志法是经常用来命名变量的一种方法。在这个方法中,变量要带上一个说明他们类型的前缀。比如

 

int nValue; // the n before Value represents that this is an integer

bool bValue; // b means boolean

char chValue; // ch means char

double dValue; // d means double

float fValue; // f means float

 

我们在涉及到多种数字类型的时候自然会深入讲匈牙利标志法。

小测验

根据规则哪个声明是不符合的:

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;

Hide Solution

2) Variable names should not start with an underscore.
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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HDCP(High-bandwidth Digital Content Protection)是一种数字内容保护技术,用于保护高清内容的传输和播放。HDCP 1.4和HDCP 2.2是两个不同版本的HDCP标准,它们之间有以下区别: 1. 带宽:HDCP 1.4具有最高10.2 Gbps的带宽,适用于1080p和一些2K分辨率的视频。而HDCP 2.2则具有高达18 Gbps的带宽,能够支持更高的分辨率,例如4K和8K视频。 2. 加密算法:HDCP 1.4使用RSA 1024位的非对称加密和AES 128位的对称加密算法。而HDCP 2.2采用更强大的RSA 3072位的非对称加密和AES 128位的对称加密算法,提供更高的安全性。 3. 兼容性:HDCP 1.4是HDCP标准的早期版本,较老的设备通常只支持该版本。而HDCP 2.2是较新的标准,对于支持4K播放的设备来说更为常见。因此,如果想要在支持HDCP 2.2的设备上播放4K内容,就需要确保所有连接的设备都支持HDCP 2.2。 4. 快速关机恢复:HDCP 2.2引入了快速关机恢复(Fast Fallback)功能。当HDCP加密链路上的设备意外断开连接时,HDCP 2.2可以更快地重新建立起加密链路,并恢复正常的内容传输,而HDCP 1.4则需要较长的时间来重新建立加密连接。 综上所述,HDCP 1.4和HDCP 2.2在带宽、加密算法、兼容性和快速关机恢复等方面存在明显的区别。HDCP 2.2是更高级、更安全、更适用于4K内容的标准。在选择设备和传输高清内容的时候,了解它们之间的区别是非常重要的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值