摘要:
A definition of a variable allocates storage for the variable and may also specify an initial value for the variable. There must be one and only one definition of a variable in a program.
A declaration makes known the type and name of the variable to the program. A definition is also a declaration: When we define a variable, we declare its name and type. We can declare a name without defining it by using the extern keyword. A declaration that is not also a definition consists of the object's 阅读全文
随笔档案-2010年10月12日
[C++再学习系列] 变量与声明时初始化
2010-10-12 11:24 by zhenjing, 1456 阅读, 收藏, 编辑
摘要:
未初始化的变量常常会导致一些奇怪的bug,有时还真不好调式。养成在初始化变量的习惯有利于减少编程错误,提高程序质量。
C++提供了构造和析构函数,其中构造函数就是为初始化而设计的;对于内置变量(char,int,long等)的初始化C++无能为力,其默认行为是未初始化,故其值依赖于变量类型(是否为static)和编译器。
本文中将讨论两类基本变量:标量和数组,标量指单一变量,数组本质上就是一整块内存空间(别忘了memset)。其他数据结构均基于这两类基本变量,一般由库提供,比如著名的C++ STL容器。 阅读全文