c++官方文档-命名空间

#include<stdio.h>
#include<iostream>
#include<queue>
#include<map>
#include<memory.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <climits>
#include <sstream>
#include <cstdlib>
using namespace std;

/**
 * 命名空间
 *
 *namespace identifier
 *{
 *  named_entities
 *}
 */
namespace foo
{
int value()
{
	return 5;
}
}

namespace bar
{
const double pi = 3.1416;
double value()
{
	return 2 * pi;
}
}

namespace first
{
int x = 5;
int y = 10;
}

namespace second
{
double x = 3.1416;
double y = 2.7183;
}

//int main()
//{
//	cout << foo::value() << '\n';
//	cout << bar::value() << '\n';
//	cout << bar::pi << '\n';
//	using first::x;
//	using second::y;
//	cout << x << endl;
//	cout << y << endl;
//	cout << first::y << endl;
//	cout << second::x << endl;
//	return 0;
//}
int x;
int main()
{
	//using namespace和using的作用域
	/*
	{
		using namespace first;
		cout << x << '\n';
	}
	{
		using namespace second;
		cout << x << '\n';
	}
	//命名空间别名namespace new_name = current_name;
*/
	//静态存储和自动存储
	int y;
	cout << x << '\n';
	cout << y << '\n';
	return 0;
}
/**
 *
 * The storage for variables with global or namespace scope is allocated for the entire duration of the program. This is known as static storage, and it contrasts with the storage for local variables (those declared within a block). These use what is known as automatic storage. The storage for local variables is only available during the block in which they are declared; after that, that same storage may be used for a local variable of some other function, or used otherwise.
 *
 *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,自动存储变量值未初始化值是未确定
 *
 */

  

posted on 2017-12-10 17:25 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8017464.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值