名称空间 (相关) (例子)

名称空间是开放的,所以可以把名称加入到已有的名称空间中
///////////////////////////////////////////////////// 
名称空间内可包含名称空间
ep:
namespace elem
{
    double add(int x)
    int pal;
    struct{…};
    namespace  fire
   {
       int flame;
       …    
   }
}
/**/
这里flame是 elem::fire::flame 所以可以用 using namespace elem::fire
////////////////////////////////////////////////////////
名称空间中可以用using编译和using声明
ep:
namespace myth
{
  using jill::fetch;
  using std::cout;
}
/**/
这里的fetch      可以是myth::fetch     也是   jill::fetch
///////////////////////////////////////////////
using声明
ep: using std::cout;     // ::域解析操作符
某名称在某个函数中声明了 则无法再使用 using声明  
/**/
易检测名称冲突
////////////////////////////////////////////////
using编译
ep:using namespace std;
using编译导入已经在函数的名称 则局部名称将隐藏名称空间中名称
/**/
无法预料名称冲突
////////////////////////////////////////////////
名称空间可以被创建别名
ep:namespace my_very_good_friend{……}
      
        namespace MVGF=my_very_good_friend;
已此技术简化嵌套名称空间使用
ep:namespace MEF=myth::elem::fire;
       using MEF::flame
////////////////////////////////////////////////////////
在已命名 名称空间 中申明变量,代替外部全局变量

在未命名 名称空间 中申明变量,代替内部全局变量
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////namesp.h////////////////////////////////////////////////
#ifndef _EdwarD_D_  //定义两个名称空间
#define _EdwarD_D_
namespace pers
{
 const int LEN=40;
 struct person
 {
  char fname[LEN];
  char lname[LEN];
 };
 void getperson(person &);
 void showperson(const person &);
}

namespace debts
{
 using pers::getperson;  //  因该用using namespace pers;但vc6通不过 测试vc8与devcpp通过
 using pers::LEN;
 using pers::person;
 using pers::showperson;
 struct debt
 {
  person name;
  double amount;
 };
 void getdebt(debt &);
 void showdebt(const debt &);
 double sumdebts(const debt ar[],int n);
}
#endif
///////////////////////////////namesp.cpp//////////////////////////////////////
提供头文件中函数原型的对应定义 利用名称空间的开放性
///////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "namesp.h"
/***************************************/
/***************************************/
namespace pers
{
 using std::cout;
 using std::cin;
 /***************************************/
 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.fname<<","<<rp.lname<<std::endl;
 }
}
/***************************************/
/***************************************/
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;
  for (int i=0;i<n;i++)
   total+=ar[i].amount;
  return total;
 }
}
//////////////////////////////main.cpp//////////////////////////////////////
#include <iostream>
#include "namesp.h"

void other(void);
void another(void);
int main(void)
{
 using debts::debt;
 using debts::showdebt;
 
 debt golt={{"benny","goatsniff"},120.0};
 showdebt(golt);
 other();
 another();

 return 0;
}

void other(void)
{
 using std::cout;
 using std::endl;
 using namespace debts;
 person dg={"dogg","ggg"};
 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 "<<sumdebts(zippy,3)<<endl;

 return;
}

void another(void)
{
 using pers::person;

 person coll={"milo","rigiht"};
 pers::showperson(coll);
 std::cout<<std::endl;
}

posted @ 2007-02-11 05:45  Edward Xie  阅读(169)  评论(0)    收藏  举报