目录
(一)命名空间的概念
(1)什么是命名空间?
不知各位是否注意到,在编写程序时要输入、输出时总会遇到 std::cin 和 std::cout。众所周知 cin和cout 是输入输出流,那么std,:: 是什么呢?std 就是一个命题空间 所有的标准库内容都放在里面;:: 则是作用域符号
(2)命名空间有什么用?
假如你们身边有两个名为子涵的朋友,单靠名的话很难区分你叫的是那个子涵,但此时你若加上他们的姓氏,一个叫周子涵(周::子涵),一个叫张子涵 (张::子涵)。区分就变得简单了。命名空间就像姓氏,帮助你用来区分同名的变量。
(二)命名空间的使用
(1)基础使用
1. 编写命名空间
我们可以用namespace自己定义命名空间,将相关的变量、函数封装到一个命名空间中,避免与其他同名的变量发生冲突。
- 示例代码:
namespace Nspace_1
{
int num;
}
namespace Nspace_2
{
int num;
}
int main(int argc, char const *argv[])
{
Nspace_1::num = 10;
Nspace_2::num = 20;
std::cout << "num_1 = " << Nspace_1::num << "\n" << "num_2 = " << Nspace_2::num <<std::endl;
return 0;
}
2. 简化命名空间
在编写输出时总是忘记在cout前加上std::导致报错,如何偷个小懒不加std::呢?这时候就要用到我们的using指示;using指示用来将一个命名空间中的名字引入到当前作用域(可能会存在命名冲突);那么using怎么用呢?各位观众老爷请看大屏幕。
- 示例代码:
// 将全部名字引入到当前作用域
using namespace std;
int main(int argc, char const *argv[])
{
int num = 0;
cout << "num = " << num << endl; // 会发现此时即使不加std::也不会报错
return 0;
}
也可以单独使用 如下:
// 将部分名字引入到当前作用域
using std::cout;
int main(int argc, char const *argv[])
{
int num = 0;
cout << "num = " << num << std::endl;
return 0;
}
3. 命名空间冲突问题
3.1 命名空间与命名空间冲突
- 示例代码:
#include <iostream>
#include <string>
namespace Nspace_1
{
int num;
}
namespace Nspace_2
{
int num;
}
// 将std中的全部名字引入当前作用域
using namespace std;
// 将Nspace_1中全部名字加入当前作用域
using namespace Nspace_1;
int main(int argc, char const *argv[])
{
// 因为Nspace_1中的所有名字加入到了当前作用域 即使不加 “Nspace_1” 系统默认 num 就是 Nspace_1::num
// 如果此时 using namespace Nspace_2; 那么系统就无法辨认 num 是 Nspace_1中的num 还是 Nspace_2中的num
num = 10;
Nspace_2::num = 20;
cout << "Nspace_1 = " << num << endl;
cout << "Nspace_2 = " << Nspace_2::num << endl;
return 0;
}
3.2 命名空间与局部变量冲突
命名空间与局部变量冲突优先使用局部变量
- 示例代码
#include <iostream>
#include <string>
using namespace std;
namespace Nspace
{
int num;
}
using namespace Nspace;
int main(int argc, char const *argv[])
{
Nspace::num = 10;
int num = 20; //局部定义一个num *优先使用局部变量*
cout << "Nspace::num = " << Nspace::num << endl;
cout << "num = " << num << endl;
return 0;
}
3.3 命名空间与全局变量冲突
命名空间与全局变量冲突优先使用全局变量
#include <iostream>
#include <string>
using namespace std;
namespace Nspace
{
int num;
}
using namespace Nspace;
int num = 50;
int main(int argc, char const *argv[])
{
Nspace::num = 10;
cout << "Nspace::num = " << Nspace::num << endl;
cout << "num = " << ::num << endl; // 使用"::num"访问全局变量
return 0;
}
(2)拓展使用
1. 命名空间封装函数
在C++中,命名空间不仅可以封装变量,还可以封装函数。该方式可以将不同功能模块分开,避免命名冲突。同时我们还能更加轻松的管理同名的函数,变量,使代码结构更加清晰
- 示例代码:
using namespace std;
//定义命名空间
namespace Nspace_1
{
int num;
void func(int num);
}
namespace Nspace_2
{
int num;
void func(int num);
}
// 定义命名空间中的函数
void Nspace_1::func(int num_1)
{
cout << "我是一号函数 num = " << num_1 << endl;
}
void Nspace_2::func(int num_2)
{
cout << "我是二号函数 num = " << num_2 << endl;
}
int main(int argc, char const *argv[])
{
Nspace_1::num = 1;
Nspace_2::num = 2;
Nspace_1::func(Nspace_1::num);
Nspace_2::func(Nspace_2::num);
return 0;
}
2. 命名空间嵌套
在C++中,命名空间不仅可以独立存在,还可以嵌套在其他命名空间中。 帮助我们细化功能模块的管理
- 示例代码:
#include <iostream>
#include <string>
using namespace std;
// 定义命名空间
namespace Nspace_father
{
string str;
namespace Nspace_son_1
{
string str;
}
namespace Nspace_son_2
{
string str;
}
}
int main(int argc, char const *argv[])
{
Nspace_father::str = "我是爸爸";
Nspace_father::Nspace_son_1::str = "我是儿子一号";
Nspace_father::Nspace_son_2::str = "我是儿子二号";
cout << "fater : " << Nspace_father::str << endl;
cout << "son_1 : " << Nspace_father::Nspace_son_1::str << endl;
cout << "son_2 : " << Nspace_father::Nspace_son_2::str << endl;
return 0;
}
551

被折叠的 条评论
为什么被折叠?



