赞
踩
人生重要的不是所站的位置,而是所朝的方向;
using namespace std;
int a = 10;//全局变量
void test01()
{
int a = 20;//局部变量
cout<<"局部变量a = "<<a<<endl;//优先选择局部变量
//::作用域运算符(c++独有)
cout<<"全局变量a = "<<::a<<endl;//取全局变量
}
namespace 可以自己修改或创建
//定义一个名字为A的命名空间(变量、函数)
namespace A {
int a = 100;
}
namespace B {
int a = 200;
}
void test02()
{
//A::a a是属于A中
cout<<"A中a = "<<A::a<<endl;//100
cout<<"B中a = "<<B::a<<endl;//200
}
namespace A {
int a = 1000;
namespace B {
int a = 2000;
}
}
void test03()
{
cout<<"A中的a = "<<A::a<<endl; //1000
cout<<"B中的a = "<<A::B::a<<endl; //2000
}
namespace A {
int a = 100;
int b = 200;
}
//将c添加到已有的命名空间A中
namespace A {
int c = 300;
}
void test04()
{
cout<<"A中a = "<<A::a<<endl;//100
cout<<"A中c = "<<A::c<<endl;//200
}
namespace A { int a=100;//变量 void func()//函数 { cout<<"func遍历a = "<<a<<endl; } } void test05() { //变量的使用 cout<<"A中的a = "<<A::a<<endl; //函数的使用 A::func(); }
namespace A { int a=100;//变量 void func(); } void A::func()//成员函数 在外部定义的时候 记得加作用域 { //访问命名空间的数据不用加作用域 cout<<"func遍历a = "<<a<<endl; } void funb()//普通函数 { cout<<"funb遍历a = "<<A::a<<endl; } void test06() { A::func(); funb(); }
运行结果:
namespace{
int a =10;
void func(){cout << "hello namespace "<< endl}
}
void test(){
cout << "a:" << a << endl;
func();
}
namespace veryLongName{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
namespace shortName = veryLongName;
cout << "veryLongName::a : " << shortName::a << endl;
veryLongName::func();
shortName::func();
}
namespace veryLongName {
int a=100;
void func(){cout<<"hello namespace"<<endl;}
}
void test07()
{
//使用veryLongName命名空间
using namespace veryLongName;
//出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
cout<<"a = "<<a<<endl;
func();
}
namespace veryLongName {
int a=100;
void func(){cout<<"hello namespace"<<endl;}
}
void test07()
{
int a=200;
//使用veryLongName命名空间
using namespace veryLongName;
//出现的变量 从veryLongName命名空间中找 找不到 从其他地方中
cout<<"a = "<<a<<endl;//访问的是局部变量中的a
cout<<"a = "<<veryLongName::a<<endl;//访问的是veryLongName的a
func();
}
using直接使用 命名空间中的成员 会和 局部变量冲突
namespace veryLongName {
int a = 100;
void func(){cout << "hello namespace" << endl;}
}
void test01()
{
//using直接使用 命名空间的成员会和局部变量冲突
int a = 200;
//指明 使用命名空间中的具体成员 容易和其他变量冲突
using veryLongName::a;//err
cout << "a = " << a << endl;
//但是func使用的时候 必须加作用域
veryLongName::func();
}
using直接使用 命名空间中的成员 不会和 全局变量冲突
namespace veryLongName { int a=100; void func(){cout<<"hello namespace"<<endl;} } int a = 200; void test07() { //using直接使用 命名空间中的成员 不会和 全局变量冲突 using veryLongName::a; cout<<"命名空间中a = "<<a<<endl;//命名空间中的成员 100 cout<<"全局变量中a = "<<::a<<endl;//200 //但是func使用的时候 必须加作用域 veryLongName::func(); }
namespace A {
//函数重载 函数名+参数 组合代表是函数的入口地址
void func(){cout<<" 无参的func"<<endl;}
void func(int a){cout<<" int的func"<<endl;}
void func(int a,int b){cout<<" int int的func"<<endl;}
}
void test08()
{
//using指明 使用 A中的func 会对 所有的func起作用
using A::func;
func();
func(10);
func(10,20);
}
运行结果:
namespace A {
int a = 10;
}
namespace B {
int a = 20;
}
void test09()
{
//此处的a 不知道是A还是B中a
//cout<<"a = "<<a<<endl;//err
//解决方法
cout<<"A::a = "<<A::a<<endl;//100
cout<<"B::a = "<<B::a<<endl;//200
}
namespace A
{
int a = 10;
void func(){cout<<"func"<<endl;}
}
1、命名空间的定义( 不能在 函数内 定义命名空间)
2、使用命名空间的成员 最安全的方式 命名空间名::成员名
3、using namespace 命名空间名;使用整个命名空间 (重要)
using namespace A;
4、单独 使用命名空间中的具体成员:using 命名空间名::成员名;
using A::a;
5、说明一下main中的std
#include <iostream>
//使用标准的命名空间std
//std中所有成员名 可以直接使用
//cout endl cin都是命名空间std的成员
using namespace std;
int main(int argc, char *argv[])
{
std::cout << "Hello World!" << std::endl;
cout << "Hello World!" << endl;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。