我们都知道,创建的每一个类、函数和变量都只能在一定的区域内使用。
而我们所知道最大的区域是全局作用域,最小的区域是一个代码块,例如:
{
int a = 20;
}//a在此处已经不存在
随着程序变得越来越复杂,全局作用域里的东西越来越多,尤其是在使用外部函数库时。
这可就会导致必须把名字取得很长才会让它们不重名。
这正是“命名空间”(namespace)的作用。
命名空间其实就是由用户定义的范围,同一个命名空间里的东西只要在这个命名空间有独一无二的名字就行了。
因此,如果某个程序有许多不同的头文件或已编译文件,它们又各自声明了许多的东西,命名空间可以为它们提供保护。
创建命名空间的格式
namespace myNamespace
{
//全部东西
}
注意在最末尾不用加上分号。
正如我们刚才讲过的那样,命名空间可以让你使用同一个标识符而不会导致冲突:
namespace author
{
std::string person;
}
namespace programmer
{
std::string person;
}
现在我们把Rational类的定义放到它自己的命名空间里去,这样一来,我们就再也不用担心它会把与也叫做Rational的其他东西发生冲突了。来个例子
头文件
//Rational.h
//Create by 孙浩天
//这个头文件声明了有理数类(Rational class)
//类里对四则运算进行重载,以实现分数运算
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
namespace myMath
{
class Rational
{
public:
Rational(int num, int denom); //num表示分子,denom表示分母
Rational operator+(Rational rhs); //rhs = right hand side表示重载右边的参数
Rational operator-(Rational rhs);
Rational operator*(Rational rhs);
Rational operator/(Rational rhs);
void print();
private:
void normalize(); //负责对分数的简化处理
int numerator; //分子
int denominator; //分母
friend std::ostream& operator<<(std::ostream& os, Rational f);
};
}
#endif
rational.cpp
#include <iostream>
#include <stdlib.h>
#include "rational.h"
namespace myMath
{
Rational::Rational(int num, int denom)
{
numerator = num;
denominator = denom;
normalize();
}
//normalize()对分数进行简化操作包括:
//1. 只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2 == -1/2
//2. 利用欧几里得算法(辗转求余原理)将分数进行简化,如2/10 => 1/5
void Rational::normalize()
{
//确保分母为正
if (denominator < 0)
{
numerator = -numerator;
denominator = -denominator;
}
//欧几里得算法
int a = abs(numerator);
int b = abs(denominator);
//求出最大公约数
while (b > 0)
{
int t = a % b;
a = b;
b = t;
}
//分子、分母分别除以最大公约数得到最简化分数
numerator /= a;
denominator /= a;
}
Rational Rational::operator+(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = a * d + b * c;
int f = b * d;
return Rational(e, f);
}
Rational Rational::operator-(Rational rhs)
{
rhs.numerator = -rhs.numerator;
return operator+(rhs);
}
Rational Rational::operator*(Rational rhs)
{
int a = numerator;
int b = denominator;
int c = rhs.numerator;
int d = rhs.denominator;
int e = numerator * rhs.numerator;
int f = denominator * rhs.denominator;
return Rational(e, f);
}
Rational Rational::operator/(Rational rhs)
{
int t = rhs.numerator;
rhs.numerator = rhs.denominator;
rhs.denominator = t;
return operator*(rhs);
}
void Rational::print()
{
if (numerator % denominator == 0)
{
std::cout << numerator / denominator;
}
else
{
std::cout << numerator << "/" << denominator;
}
}
}
main.cpp
#include <iostream>
#include "rational.h"
using namespace myMath;
int main()
{
Rational f1(2, 16);
Rational f2(7, 8);
//测试有理数加法运算
Rational res = f1 + f2;
f1.print();
std::cout << " + ";
f2.print();
std::cout << " = ";
res.print();
std::cout << std::endl;
//测试有理数减法运算
res = f1 - f2;
f1.print();
std::cout << " - ";
f2.print();
std::cout << " = ";
res.print();
std::cout << std::endl;
//测试有理数乘法运算
res = f1 * f2;
f1.print();
std::cout << " * ";
f2.print();
std::cout << " = ";
res.print();
std::cout << std::endl;
//测试有理数除法运算
res = f1 * f2;
f1.print();
std::cout << " / ";
f2.print();
std::cout << " = ";
res.print();
std::cout << std::endl;
return 0;
}
当然,在main函数那里也可以不用using namespace myMath,在下面用myMath::,就像原本用std::一样。下面这种可能更好。
使用命名空间
如果某个东西是在命名空间里定义的,程序将不能立刻使用它。
这正是命名空间的全部意义所在:把东西放在它们自己的小盒子里,不让它们与可能有着相同名字的其他东西发生冲突。
想要访问在某个命名空间里定义的东西,有三种方法可供选择。
第一种:作用域
std::cout << "I love fishc.com!";
第二种:使用using指令
using namespace std;
执行这条语句后,在std命名空间里定义的所有东西都可以使用,我们便可以想下面直接使用:
cout << "I love fishc.com!";
不过,把命名空间里的东西带到全局作用域里,跟我们使用命名空间的本意相违背!
第三种:用一个using指令只把你需要的特定命名从命名空间提取到全局作用域:
using std::cout;
cout << "I love fishc.com!";
最后请务必注意,using指令的出现位置决定着从命名空间里提取出来的东西能在哪个作用域内使用。
如果你把它放在所有函数声明的前面,它将拥有全局性,如你把它放在某个函数里,那么它将只在这一函数里可以使用。