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;而动态存储,局部变量,是没有初始化的,这个值是不确定的。
这个要理解好。