C++ Primer4 Chapter 2. Variables and Basic Types
2010-11-11 00:02
357 查看
1, 定义字面值整数常量可以使用以下三种进制中的任一种:十进制、八进制和十六进制。以 0
(零)开头的字面值整数常量表示八进制,以 0x
或 0X
开头的表示十六进制。类似地,可通过在数值后面加 U
或 u
定义 unsigned
类型。同时加 L
和
U
就能够得到 unsigned long
类型的字面值常量。
常可以用十进制或者科学计数法来表示浮点字面值常量。使用科学计数法时,指数用 E
或者 e
表示。默认的浮点字面值常量为
double
类型。在数值的后面加上 F
或 f
表示单精度。同样加上 L
或者
l
表示扩展精度(再次提醒,不提倡使用小写字母l)。
2, C++ 支持两种初始化变量
的形式:复制初始化
和直接初始化
。复制初始化语法用等号(=
),直接初始化则是把初始化式放在括号中:
int ival(1024); // direct-initialization
int ival = 1024; // copy-initialization
3,
声明
用于向程序表明变量的类型和名字。定义也是声明:当定义变量时我们声明了它的类型和名字。可以通过使用extern关键字声明变量名而不定义它。不定义变量的声明包括对象名、对象类型和对象类型前的关键字extern:
extern int i; // declares but does not define
i
int i; // declares and defines
i
extern
声明不是定义,也不
分配存储空间。事实上,它只是说明变量定义在程序的其他地方。程序中变量可以声明多次,但只能定义一次。只有当声明也是定义时,声明才可以有初始化式,因为只有定义才分配存储空间。初始化式必须要有存储空间来进行初始化。如果声明有初始化式,那么它可被当作是定义,即使声明标记为
extern:
extern double pi = 3.1416; // definition
虽然使用了 extern
,但是这条语句还是定义了 pi
,分配并初始化了存储空间。只有当
extern
声明位于函数外部时,才可以含有初始化式。
extern double pi = 3.1416; // definition
extern double pi; // ok: declaration not definition
extern double pi = 3.1416; // error: redefinition of
pi
4,
在全局作用域里定义非 const
变量时,它在整个程序中都可以访问。与其他变量不同,除非特别说明,在全局作用域声明的 const
变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。非 const
变量默认为 extern
。要使 const
变量能够在其他的文件中访问,必须地指定它为 extern
。
// file_1.cc
// defines and initializes a
const
that is accessible to other files
extern const int bufSize = fcn();
// file_2.cc
extern const int bufSize; // uses
bufSize
from
file_1
// uses
bufSize
defined in
file_1
for (int index = 0; index != bufSize; ++index)
// ...
5, 引用是一种复合类型
,是对象的别名,通过在变量名前添加“&
”符号来定义,引用主要用作函数的形式参数。引用必须
用与该引用同类型的对象初始化:
int ival = 1024;
int &refVal = ival; // ok:
refVal
refers to
ival
int &refVal2; // error: a reference must be initialized
int &refVal3 = 10; // error: initializer must be an object
const
引用
是指向 const
对象的引用:
const int ival = 1024;
const int &refVal = ival; // ok: both reference and object are
const
int &ref2 = ival; // error: non
const
reference to a
const
object
6, 枚举的定义包括关键字 enum
,其后是一个可选的枚举类型名,和一个用花括号括起来、用逗号分开的枚举成员
列表。默认地,第一个枚举成员赋值为 0,后面的每个枚举成员赋的值比前面的大 1。
enum open_modes {input, output, append};
(零)开头的字面值整数常量表示八进制,以 0x
或 0X
开头的表示十六进制。类似地,可通过在数值后面加 U
或 u
定义 unsigned
类型。同时加 L
和
U
就能够得到 unsigned long
类型的字面值常量。
常可以用十进制或者科学计数法来表示浮点字面值常量。使用科学计数法时,指数用 E
或者 e
表示。默认的浮点字面值常量为
double
类型。在数值的后面加上 F
或 f
表示单精度。同样加上 L
或者
l
表示扩展精度(再次提醒,不提倡使用小写字母l)。
2, C++ 支持两种初始化变量
的形式:复制初始化
和直接初始化
。复制初始化语法用等号(=
),直接初始化则是把初始化式放在括号中:
int ival(1024); // direct-initialization
int ival = 1024; // copy-initialization
3,
声明
用于向程序表明变量的类型和名字。定义也是声明:当定义变量时我们声明了它的类型和名字。可以通过使用extern关键字声明变量名而不定义它。不定义变量的声明包括对象名、对象类型和对象类型前的关键字extern:
extern int i; // declares but does not define
i
int i; // declares and defines
i
extern
声明不是定义,也不
分配存储空间。事实上,它只是说明变量定义在程序的其他地方。程序中变量可以声明多次,但只能定义一次。只有当声明也是定义时,声明才可以有初始化式,因为只有定义才分配存储空间。初始化式必须要有存储空间来进行初始化。如果声明有初始化式,那么它可被当作是定义,即使声明标记为
extern:
extern double pi = 3.1416; // definition
虽然使用了 extern
,但是这条语句还是定义了 pi
,分配并初始化了存储空间。只有当
extern
声明位于函数外部时,才可以含有初始化式。
extern double pi = 3.1416; // definition
extern double pi; // ok: declaration not definition
extern double pi = 3.1416; // error: redefinition of
pi
4,
在全局作用域里定义非 const
变量时,它在整个程序中都可以访问。与其他变量不同,除非特别说明,在全局作用域声明的 const
变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。非 const
变量默认为 extern
。要使 const
变量能够在其他的文件中访问,必须地指定它为 extern
。
// file_1.cc
// defines and initializes a
const
that is accessible to other files
extern const int bufSize = fcn();
// file_2.cc
extern const int bufSize; // uses
bufSize
from
file_1
// uses
bufSize
defined in
file_1
for (int index = 0; index != bufSize; ++index)
// ...
5, 引用是一种复合类型
,是对象的别名,通过在变量名前添加“&
”符号来定义,引用主要用作函数的形式参数。引用必须
用与该引用同类型的对象初始化:
int ival = 1024;
int &refVal = ival; // ok:
refVal
refers to
ival
int &refVal2; // error: a reference must be initialized
int &refVal3 = 10; // error: initializer must be an object
const
引用
是指向 const
对象的引用:
const int ival = 1024;
const int &refVal = ival; // ok: both reference and object are
const
int &ref2 = ival; // error: non
const
reference to a
const
object
6, 枚举的定义包括关键字 enum
,其后是一个可选的枚举类型名,和一个用花括号括起来、用逗号分开的枚举成员
列表。默认地,第一个枚举成员赋值为 0,后面的每个枚举成员赋的值比前面的大 1。
enum open_modes {input, output, append};
相关文章推荐
- Chapter 2. Variables and Basic Types
- C++ Primer 总结之Chap2 Variables and basic types
- C++ Primer 4th Notes - Chapter 2 Variables and Basic Types
- Chapter 2 Variables and Basic Types
- [C++] Variables and Basic Types
- C++ Variables and Basic Types Notes
- C++ Primer notes: Chapter 2. Variables and Basic Types
- Chapter 2: Variables and Basic Types
- 2-Variables and Basic Types
- Chapter 2 Variables and Basic Types
- Chapter 2. Variables and Basic Types
- CUDA系列学习(二)CUDA memory & variables - different memory and variable types
- CUDA系列学习(二)CUDA memory & variables - different memory and variable types
- variables and types(变量和数据类型)
- Computing for Data Analysis by Roger D. Peng @ Hopkins: Notes -- Data Types and Basic Operations
- More on Variables and Data Types(Chapter 10 of Programming in Objective-C 2.0)
- 1.2 C++变量和数据类型 (Variables and Data types )
- CUDA系列学习(二)CUDA memory & variables - different memory and variable types
- 2. C# - Learning About Built-In Types and Variables
- php tutorial Constant,Variables and data types