C++ 命名空间

C++ 命名空间

namespace

可以自定义自己的命名空间,看这个代码你就能很好的理解:
#include <iostream>
using namespace std;
namespace My
{
    int x=3;
}

int main()
{
    cout << My::x <<endl;
}

using

这个可以直接使用有关命名空间,而不用再使用时声明它所在的空间。

注意,必须在你定义的namespace后面使用这个,否则无效。

同时,如果在函数中声明的本地变量,这本地变量会优先,覆盖你所声明的,这个是很合理的。

#include <iostream>
using namespace std;

namespace My
{
    int x=3;
}

using namespace My;
int main()
{
    cout << x <<endl;
}

Namespace aliasing

Existing namespaces can be aliased with new names, with the following syntax:

namespace new_name = current_name;

The std namespace

All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace. Most examples in these tutorials, in fact, include the following line.

所有C++标准函数的函数名都放在名为 std 的命名空间了,你可以直接声明。

Storage classes 存储类

But there is another substantial difference between variables with static storage and variables with automatic storage:
- Variables with static storage (such as global variables) that are not explicitly initialized are automatically initialized to zeroes.
- Variables with automatic storage (such as local variables) that are not explicitly initialized are left uninitialized, and thus have an undetermined value.

两种存储的不同:
静态存储,即全局变量:自从存储其值为0;而动态存储,局部变量,是没有初始化的,这个值是不确定的。

这个要理解好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值