namespace cplusplus_primer {
class matrix { /* ... */ };
void inverse ( matrix & );
matrix operator+ ( const matrix &m1, const matrix &m2 )
{ /* ... */ }
const double pi = 3.1416;
}
在名字空间cplusplus_primer 中声明的类的名字是
cplusplus_primer::matrix
函数的名字是
cplusplus_primer::inverse()
常量的名字是
cplusplus_primer::pi
============
为访问全局声明我们必须使用域操作符::max 下面是实现
#include <iostream>
const int max = 65000;
const int lineLength = 12;
void fibonacci( int max )
{
if ( max < 2 ) return;
cout << "0 1 ";
int v1 = 0, v2 = 1, cur;
for ( int ix = 3; ix <= max; ++ix ) {
cur = v1 + v2;
if ( cur > ::max ) break;
cout << cur << " ";
v1 = v2;
v2 = cur;
if (ix % lineLength == 0) cout << endl;
}
}
============
在嵌套名字空间MatrixLib 中声明的类的名
字是
cplusplus_primer::MatrixLib::matrix
函数的名字是
cplusplus_primer::MatrixLib::inverse
===========
由于namespace的概念,使用C++标准程序库的任何标识符时,可以有三种选择:
1、直接指定标识符。例如std::ostream而不是ostream。完整语句如下:
std::cout << std::hex << 3.4 << std::endl;
2、使用using关键字。
using std::cout;
using std::endl;
以上程序可以写成
cout << std::hex << 3.4 << endl;