• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LOFLY
终其一生,编织快乐
博客园    首页    新随笔    联系   管理    订阅  订阅

C++之名称空间 namespacce

C++之名称空间 namespacce

C++之名称空间 namespacce

namespace name{
   double pail;
   void fetch();
   struct Well{…};
}

名称空间可以是全局的,也可以位于另一个名称空间中,但不能在代码块中。
默认情况下,名称空间中声明的名称链接性是外部的。全局名称空间global namespace,它对应于文件级声明区域。
名称空间是开放的,可以将已有的名称加入名称空间中。

namespace name{
	char * goose(const char *);
}

将goose添加到name名称列表中。
访问名称空间中的名称。name::pail;

3.1 using 声明和using 编译指令

using声明使特定的标识符可用,using编译指令使整个名称空间可用。
using Jill :: fetch; // 使用using 声明

在函数外部使用using Jill :: fetch; 将把fetch添加到全局的名称空间中。

using声明使得一个名称可用,using编译指令使得所有的名称都可用。
using namespace name; 使得name中的所有名称可用。
在函数中使用using编译指令,将使其中的名称在该函数中可用。

int func(){
using namespace name;
	return 0;
}

使用using声明时,就好像声明了相应的名称一样。如果某个名称已经在函数中声明了,则不能使用using声明导入相同的名称。

注:假设名称空间和声明区域定义了相同的名称。如果使用using声明将名称空间的名称导入该声明区域,则这两个名称会发生冲突,从而出错。如果使用using编译指令将该名称空间的名称导入该声明区域,则局部版本将隐藏名称空间版本。
一般来说,使用using声明比using编译指令更安全。

using namespace std; //将名称空间std中的所有内容导出到全局名称空间中。
或者
std::cout << “Hello” << endl; 
或者
using std::cin;
int x;  cin>> x;

名称空间的其他特性:
名称空间声明进行嵌套。

namespace elements{
	namespace fire{
		int flame;
		…
}
}

访问flame, usinig namespace elements::fire;
也可以在名称空间中使用using编译指令和using声明

namespace myth{
	using Jill::fetch;
	using namespace elements;
}

假设要访问fetch,则需要这样 myth::fetch;
名称空间可以取别名: 如

namespace MEF = myth::elements::fires;
using MEF::flame; 

匿名的名称空间: 提供了链接性为内部的静态变量的替代品。

namespace
{
	int ice;
    Int bandycoot;
}

就相当于下面代码

static int ice;
static int bandycool;
示例程序: (在头文件中声明命名空间,在cpp文件中定义命名空间)
namesp.h头文件
#include <string>

namespace pers{
    struct Person{
        std::string fname;
        std::string lname;
    };
    void getPerson(Person &);
    void showPerson(const Person &);
}

namespace debts{
    using namespace pers;
    struct Debt
    {
        Person name;
        double amount;
    };

    void getDebt(Debt &);
    void showDebt(const Debt &);
    double sumDebts(const Debt ar[] , int n);
}
namesp.cpp文件
#include <iostream>
#include "namesp.h"

namespace pers
{

    using std::cin;
    using std::cout;

    void getPerson(Person &rp)
    {
        cout << "Enter first name: ";
        cin >> rp.fname;
        cout << "Enter last name: ";
        cin >> rp.lname;
    }

    void showPerson(const Person &rp)
    {
        std::cout << rp.lname << " ," << rp.fname;
    }
}

namespace debts
{

    void getDebt(Debt &rd)
    {
        getPerson(rd.name);
        std::cout << "Enter debt: ";
        std::cin >> rd.amount;
    }
    void showDebt(const Debt &rd)
    {
        showPerson(rd.name);
        std::cout << ": $" << rd.amount << std::endl;
    }
    double sumDebts(const Debt ar[], int n)
    {
        double total = 0;
        for (int i = 0; i < n; i++)
        {
            total += ar[i].amount;
        
        }
        return total;
    }

}
namessp.cpp文件
#include <iostream>
#include "namesp.h"

void other(void);
void another(void);

int main(int argc, char const *argv[])
{
    using debts::Debt;
    using debts::showDebt;

    Debt golf = { {"Benny","Goatsniff"},120.0 };
    showDebt(golf);
    other();
    another();
    return 0;
}

void other(void){

    using std::cout;
    using std::endl;
    using namespace debts;

    Person dg = {"Doodles","Glister"};
    showPerson(dg);
    cout << endl;
    Debt zippy[3];
    int i;
    for ( i = 0; i < 3; i++)
    {
       getDebt(zippy[i]);
    }

    for ( i = 0; i < 3; i++)
    {
        showDebt(zippy[i]);
    }

    cout << "Total debt: $" << sumDebts(zippy,3) << endl;
    return;
}

void another(void){
    using pers::Person;
    Person collector = {"Milo","Rightshift"};
    pers::showPerson(collector);
    std::cout << std::endl;
}

运行结果:

Goatsniff ,Benny: $120
Glister ,Doodles
Enter first name: zhang
Enter last name: san
Enter debt: 34
Enter first name: li
Enter last name: si
Enter debt: 56
Enter first name: wang
Enter last name: wu
Enter debt: 34
san ,zhang: $34
si ,li: $56
wu ,wang: $34
Total debt: $124
Rightshift ,Milo

名称空间及其用途:

  • 如果开发了一个函数库或类库,将其放在一个名称空间中
  • 导入名称时,首选使用作用域解析运算符或using声明的方法
  • 对于using声明,首选将其作用域设置为局部而不是全局
posted @ 2022-08-19 10:15  编织快乐  阅读(73)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3