c++

1.) def.cpp

1 // const int age = 32;  --------------- error !!!! 只能在当前文件中访问,属于局部变量 --------------
2 extern const int age = 32;

2.) defTest.cpp

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 extern const int age;
 6 
 7 extern double height = 1.72; // ------------ warning ------------------------
 8 
 9 int main(void)
10 {
11     cout << "age: " << age << endl;
12     cout << "height: " << height << endl;
13     
14     return 0;   
15 } 

 非 const 变量默认为 extern。要使 const 变量能够在其他的文件中访问,必须地指定它为 extern。 

非 const 引用只能绑定到与该引用同类型的对象。

 

对于头文件不应该含有定义这一规则,有三个例外。头文件可以定义类、值在编译时就已知道的 const 对象和 inline 函数(第 7.6 节介绍 inline 函数)。这些实体可在多个源文件中定义,只要每个源文件中的定义是相同的。

 

当进行 string 对象和字符串字面值混合连接操作时,+ 操作符的左右操作
数必须至少有一个是 string 类型的:

posted @ 2015-01-29 13:40  挨踢淫才  阅读(152)  评论(0编辑  收藏  举报