名字空间

 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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值