|
@@ -1,126 +0,0 @@
|
|
|
-#include <iostream>
|
|
|
-
|
|
|
-using namespace std;
|
|
|
-
|
|
|
-#if 0
|
|
|
-int a = 10; //全局变量
|
|
|
-void test01()
|
|
|
-{
|
|
|
- int a = 20;//局部变量
|
|
|
-
|
|
|
- cout << "局部变量a = " << a << endl; //优先选择局部变量
|
|
|
-
|
|
|
- //::作用域运算符(C++独有)
|
|
|
-
|
|
|
- cout << "全局变量a = " << ::a << endl; //取全局变量
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-//定义一个名字为A的命名空间(变量、函数)
|
|
|
-namespace A {
|
|
|
- int a = 100;
|
|
|
-}
|
|
|
-namespace B {
|
|
|
- int a = 200;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void test02()
|
|
|
-{
|
|
|
- //A::a a是属于A中的
|
|
|
- cout << "namespace A中的a = " << A::a << endl; //100
|
|
|
- cout << "namespace B中的a = " << B::a << endl; //200
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-namespace A {
|
|
|
- int a = 1000;
|
|
|
- namespace B {
|
|
|
- int a = 2000;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void test03()
|
|
|
-{
|
|
|
- cout << "A中的a = " << A::a << endl;
|
|
|
- cout << "B中的a = " << A::B::a << endl;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-//将c添加到已有的命名空间A中
|
|
|
-namespace A{
|
|
|
- int a = 100;
|
|
|
- int b = 200;
|
|
|
-}
|
|
|
-namespace A{
|
|
|
- int c = 300;
|
|
|
-}
|
|
|
-
|
|
|
-void test04()
|
|
|
-{
|
|
|
- cout << "A中的c = " << A::c << endl;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-namespace A{
|
|
|
- int a = 100; //变量
|
|
|
- void func()
|
|
|
- {
|
|
|
- cout << "func遍历a = " <<a<<endl;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void test05()
|
|
|
-{
|
|
|
- //变量的使用
|
|
|
- cout << "A中的a = " << A::a << endl;
|
|
|
- //函数的使用
|
|
|
- A::func();
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-namespace A{
|
|
|
- int a = 100;
|
|
|
-
|
|
|
- void func();
|
|
|
-}
|
|
|
-
|
|
|
-void A::func()
|
|
|
-{
|
|
|
- cout << "func遍历a = " << a << endl;
|
|
|
-}
|
|
|
-void funb()
|
|
|
-{
|
|
|
- cout << "funb遍历a = " << A::a << endl;
|
|
|
-}
|
|
|
-void test06()
|
|
|
-{
|
|
|
- A::func();
|
|
|
- funb();
|
|
|
-}
|
|
|
-
|
|
|
-#endif
|
|
|
-
|
|
|
-namespace veryLongName{
|
|
|
- int a = 10;
|
|
|
- void func()
|
|
|
- {
|
|
|
- cout << "hello namespace" << endl;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void test()
|
|
|
-{
|
|
|
- namespace shortName = veryLongName;
|
|
|
- cout << "veryLongName::a : " << shortName::a << endl;
|
|
|
- veryLongName::func();
|
|
|
- shortName::func();
|
|
|
-}
|
|
|
-int main()
|
|
|
-{
|
|
|
- test();
|
|
|
- return 0;
|
|
|
-}
|