[初]C++ 的 声明&定义&初始化
2007-04-02 11:42
501 查看
在C++和Java中,我们在使用一个标识符之前必须对它进行声明。标识符的声明和标识符的定义并不是一回事(虽然绝大数情况下它们看起来并没有什么不同)。
在声明中,除非我们为一个标识符分配了足够的内存,否则这个标识符便没有被定义。
另外,当一个标识符作为一个变量的名称时,我们还将面临的一个问题是,这个变量在定义之时是否得到了缺省的初始值。
所以判定一条语句时候,可以用这个规则:
是否声明-->声明后是否定义(有无分配内存)-->定义后是否缺省初始化(有无初始化0)
下面用我自己写的一个很简单的例子来说明三者之间的关系:
#include <iostream>
using namespace std;
//author:snail
//date:070402
class small{
public:
int classVar[10];//[declare] [define ] [not defaylt init!]
static int arrayVar[10];
static int classStaticName;//[declare] [not define] [not init]
static int getValue(){return classStaticName;}
};
int small::classStaticName = 1;//class static member must init here!
int small::arrayVar[10] = {1};
int globleVar[10];//[declare] [define] [default init]
int main(){
int methodVar[10];//[declare] [define] [not init!]
static int methodStaticVar[10];//[declare] [define] [init!]
small s;
cout << "globleVar[0] " << globleVar[0] << endl;
cout << "globleVar[1] " << globleVar[1] << endl;
cout << "s.classVar[0] " << s.classVar[0] << endl;
cout << "s.classVar[1] " << s.classVar[1] << endl;
cout << "small::arrayVar[0] "<< small::arrayVar[0] << endl;
cout << "small::arrayVar[1] "<< small::arrayVar[1] << endl;
cout << "small::getValue() " << small::getValue() << endl;
cout << "small::classStaticName " << small::classStaticName << endl;
cout << "methodVar[0] " << methodVar[0] << endl;
cout << "methodVar[1] " << methodVar[1] << endl;
cout << "methodStaticVar[0] " << methodStaticVar[0] << endl;
cout << "methodStaticVar[1] " << methodStaticVar[1] << endl;
return 0;
}
输出结果:
globleVar[0] 0 //全局变量 在int globleVar[10]; 声明+定义+缺省初始化
globleVar[1] 0
s.classVar[0] 6204724 //类的非静态成员变量 int classVar[10]; 声明+定义+(未缺省初始化)
s.classVar[1] -1208501944
small::arrayVar[0] 0 //类的静态成员变量 static int arrayVar[10]; 声明 + (未定义) + (未缺省初始化),所以必须有另一条语句int small::arrayVar[10] = {1} 定义 + 初始化 !!!
small::arrayVar[1] 0
small::getValue() 1
small::classStaticName 1
methodVar[0] 134515496 //函数局部变量 int methodVar[10];//[declare];声明+定义+(未缺省初始化)
methodVar[1] 134516988
methodStaticVar[0] 0 //函数静态局部变量 static int methodStaticVar[10]; 声明+定义+缺省初始化
methodStaticVar[1] 0
下列的声明也不是定义:
extern int error_name; //外部变量
class Student; //照顾编译器"向前看"的特点
double d(double); //照顾编译器"向前看"的特点
作者:snail
日期:07年4月2日
参考资料:<<面向对象C++与Java比较教程>>
需要添加: 1 java的声明,定义,初始化 2 C++与java的声明,定义,初始化比较
在声明中,除非我们为一个标识符分配了足够的内存,否则这个标识符便没有被定义。
另外,当一个标识符作为一个变量的名称时,我们还将面临的一个问题是,这个变量在定义之时是否得到了缺省的初始值。
所以判定一条语句时候,可以用这个规则:
是否声明-->声明后是否定义(有无分配内存)-->定义后是否缺省初始化(有无初始化0)
下面用我自己写的一个很简单的例子来说明三者之间的关系:
#include <iostream>
using namespace std;
//author:snail
//date:070402
class small{
public:
int classVar[10];//[declare] [define ] [not defaylt init!]
static int arrayVar[10];
static int classStaticName;//[declare] [not define] [not init]
static int getValue(){return classStaticName;}
};
int small::classStaticName = 1;//class static member must init here!
int small::arrayVar[10] = {1};
int globleVar[10];//[declare] [define] [default init]
int main(){
int methodVar[10];//[declare] [define] [not init!]
static int methodStaticVar[10];//[declare] [define] [init!]
small s;
cout << "globleVar[0] " << globleVar[0] << endl;
cout << "globleVar[1] " << globleVar[1] << endl;
cout << "s.classVar[0] " << s.classVar[0] << endl;
cout << "s.classVar[1] " << s.classVar[1] << endl;
cout << "small::arrayVar[0] "<< small::arrayVar[0] << endl;
cout << "small::arrayVar[1] "<< small::arrayVar[1] << endl;
cout << "small::getValue() " << small::getValue() << endl;
cout << "small::classStaticName " << small::classStaticName << endl;
cout << "methodVar[0] " << methodVar[0] << endl;
cout << "methodVar[1] " << methodVar[1] << endl;
cout << "methodStaticVar[0] " << methodStaticVar[0] << endl;
cout << "methodStaticVar[1] " << methodStaticVar[1] << endl;
return 0;
}
输出结果:
globleVar[0] 0 //全局变量 在int globleVar[10]; 声明+定义+缺省初始化
globleVar[1] 0
s.classVar[0] 6204724 //类的非静态成员变量 int classVar[10]; 声明+定义+(未缺省初始化)
s.classVar[1] -1208501944
small::arrayVar[0] 0 //类的静态成员变量 static int arrayVar[10]; 声明 + (未定义) + (未缺省初始化),所以必须有另一条语句int small::arrayVar[10] = {1} 定义 + 初始化 !!!
small::arrayVar[1] 0
small::getValue() 1
small::classStaticName 1
methodVar[0] 134515496 //函数局部变量 int methodVar[10];//[declare];声明+定义+(未缺省初始化)
methodVar[1] 134516988
methodStaticVar[0] 0 //函数静态局部变量 static int methodStaticVar[10]; 声明+定义+缺省初始化
methodStaticVar[1] 0
下列的声明也不是定义:
extern int error_name; //外部变量
class Student; //照顾编译器"向前看"的特点
double d(double); //照顾编译器"向前看"的特点
作者:snail
日期:07年4月2日
参考资料:<<面向对象C++与Java比较教程>>
需要添加: 1 java的声明,定义,初始化 2 C++与java的声明,定义,初始化比较
相关文章推荐
- C++ 的 声明&定义&初始化
- 【static&extern,转载,绝佳】再谈下声明、定义、初始化、赋值和extern
- C++ 声明,定义与初始化的基本概念
- C++静态成员变量的声明与定义及初始化
- C++ 声明、定义、初始化、赋值
- 定义、声明、初始化、赋值 小记(C++)
- C/C++ 中变量的声明、定义、初始化的区别
- (C/C++基础)结构体_初始化_赋值_定义_声明
- C++变量 声明、初始化、定义
- <转> 模板类 声明和定义 编译 c++
- C++中的声明、定义、赋值和初始化
- C++中的声明,定义,初始化
- What's the difference between declaring and defining something in C and C++?(C和C++中,声明和定义的区别)
- 复习几个C++概念:声明与定义、传值与拷贝构造、初始化和赋值
- C++声明、定义及初始化
- C++中的变量声明、初始化和定义
- c++中的结构体:声明 定义 初始化
- C++学习&声明定义
- (总结)C++与java比较教程--7.1 声明和定义和初始化(待补充)
- c++ 声明 定义 初始化