菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#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  好吧,就是菜菜  阅读(209)  评论(0编辑  收藏  举报