天天看點

c++從入門到精通——命名空間與作用域

1 C++概述

  • C++兩大程式設計思想
  • 面向對象
  • 泛型程式設計

1.2 移植性和标準

  • ANSI 在1998制定出C++第一套标準

2 c++初識

  • 引入頭檔案 #include 标準輸入輸出流
  • 使用标準命名空間 using namespace std;
  • 标準輸出流對象 cout << “…” << 1234 << 3.14 << endl;
#include <iostream>
using namespace std;
int main()
{
  cout << "Hello World!" << endl;
  system("pause");
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 面向對象三大特性
  • 封裝、繼承、多态

3 雙冒号作用域運算符

  • ::代表作用域 如果前面什麼都不添加 代表全局作用域
#include <iostream>
using namespace std;
int atk = 1000;
void test01() {
  int atk = 2000;
  cout << "局部atk=" << atk << endl;
  // ::代表作用域  如果前面什麼都不添加 代表全局作用域
  cout << "全局atk=" << ::atk << endl;
}
int main()
{
  test01();
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域

4 namespace命名空間

  • 命名空間用途:解決名稱沖突,不同空間的方法可以相同,可以解決名稱名相同導緻沖突的問題。

    測試檔案如下:

main.cpp

#include <iostream>
#include"game1.h"
#include"game2.h"

using namespace std;
int main()
{
  A::getName();
  B::getName();
  return EXIT_SUCCESS;
}      

game1.h

#pragma once
#include<iostream>
using namespace std;
namespace A {
  void getName();
}      

game2.h

#pragma once
#include<iostream>
using namespace std;
namespace B {
  void getName();
}      

game1.cpp

#include"game1.h"
void A::getName() {

  cout << "這是A的調用" << endl;

}      

game2.cpp

#include"game2.h"
void B::getName() {

  cout << "這是B的調用" << endl;

}      

運作mian.cpp

c++從入門到精通——命名空間與作用域
  • 命名空間下可以存放 : 變量、函數、結構體、類…
#include <iostream>
namespace A {
  //命名空間下 可以放  變量、函數、結構體、類...
  int m_A=10;
  void func() {
    cout << "this is a test!" << endl;
  };
  struct Person{
    int name;
  };
  class Animal {
  public:
    int age;
  };

}

using namespace std;
int main()
{
  cout << A::m_A << endl;
  A::func() ;
  
  A::Person p;
  p.name = 2;

  A::Animal a;
  a.age = 20;

  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 命名空間必須要聲明在全局作用域
#include <iostream>
using namespace std;
void test01() {
  namespace A {

  };

}

int main()
{

  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 命名空間可以嵌套命名空間
#include <iostream>
using namespace std;
namespace S {
  namespace B {
    int age = 0;
  };
};

int main()
{
  cout << S::B::age << endl;
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 命名空間是開放的,可以随時将新成員添加到命名空間下
#include <iostream>
using namespace std;

namespace A {
  int m_A = 10;
  namespace C {
    int m_c = 100;
  };
}
// 添加成員
namespace A {
  int m_B = 1000;
};
void test()
{
  cout << "A空間下的m_A = " << A::m_A << endl;
  cout << "A空間下的m_B = " << A::m_B << endl;
}
int main()
{
  test();
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 命名空間可以匿名的
  • 當寫的命名空間的匿名的,相當于寫了 static
#include <iostream>
using namespace std;
//命名空間可以是匿名的
namespace
{
  int m_C = 1000;
  int m_D = 2000;
  //當寫的命名空間的匿名的,相當于寫了  static int m_C = 1000; static int m_D = 2000;
}
void test() {
  cout << "m_C = " << m_C << endl;
  cout << "m_D = " << ::m_D << endl;
};
int main()
{
  test();
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 命名空間可以起别名
#include <iostream>
using namespace std;
namespace A
{
  int m_C = 1000;
  int m_D = 2000;
  
}

int main()
{
  namespace B = A;
  cout << B::m_C << endl;
  cout << A::m_C << endl;

  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域

5 using聲明以及using編譯指令

#include <iostream>
using namespace std;
namespace A
{
  int id = 1;
}

namespace B
{
  int id = 3;
}
void test01()
{
  int id = 2;

  //1、using聲明
  //using KingGlory::sunwukongId ;  

  //當using聲明與 就近原則同時出現,出錯,盡量避免
  cout << id << endl;

}


int main()
{
  test01();
  return EXIT_SUCCESS;
}      
c++從入門到精通——命名空間與作用域
  • 當using聲明與 就近原則同時出現,出錯,盡量避免
#include <iostream>
using namespace std;
namespace A
{
  int id = 1;
}

namespace B
{
  int id = 3;
}
void test01()
{
  int id = 2;

  //1、using聲明
  using A::id ;  

  //當using聲明與 就近原則同時出現,出錯,盡量避免
  cout << id << endl;

}


int main()
{
  test01();
  return EXIT_SUCCESS;
}      
  • 就近原則
#include <iostream>
using namespace std;
namespace A
{
  int id = 1;
}

namespace B
{
  int id = 3;
}
void test01()
{
  int id = 2;

  //就近原則,先搜尋全局,再搜尋命名空間
  using namespace A;  

  cout << id << endl;

}


int main()
{
  test01();
  return EXIT_SUCCESS;
}