C++的命名空间的使用

来源:互联网 发布:php中post和get的区别 编辑:程序博客网 时间:2024/05/16 14:44

一、C++中的iostream和iostream.h的区别

1、#include<iostream>
int main(){
std::cout<<"c++\n";
}

2、#include<iostream.h> 
void main(){
cout<<"c++\n";
}

3、#include<iostream>
using namespace std; 
void main(){
cout<<"xx\n";
}

4、#include<iostream>
using std::cout;
void main(){
cout<<"xx\n";
}

4重方式是一样的,#include<iostream>是C++的标准方式,#include<iostream.h>是C的标准方式。

使用using namespace std表示释放std的命名空间。

二、命名空间的用法

#include<iostream.h>  
namespace A{
int a=1;
}
namespace B{
int a=2;
}
void main()
{
int a=3;
cout<<a<<"\t";
cout<<A::a<<"\t";
cout<<B::a<<endl;
}

输出的结过是3  1 2

#include<iostream.h>  
namespace A{
int a=1;
}
namespace B{
int a=2;
}
void main()
{
using namespace A;
using namespace B;
//int a=3;
cout<<a<<endl;
//cout<<A::a<<endl;
//cout<<B::a<<endl;
}

编译的时候报错。

原创粉丝点击