欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

示例詳解C++語言中的命名空間 (namespace)

 更新時間:2021年08月05日 10:47:20   作者:Yongqiang Cheng  
C++名字空間是一種描述邏輯分組的機制,也就是說,如果有一些聲明按照某種準(zhǔn)則在邏輯上屬于同一個模塊,就可以將它們放在同一個名字空間,以表明這個事實,這篇文章主要給大家介紹了關(guān)于C++語言中命名空間 (namespace)的相關(guān)資料,需要的朋友可以參考下

前言

命名空間可作為附加信息來區(qū)分不同庫中相同名稱的函數(shù)、類、變量等。命名空間即定義了上下文,命名空間就是定義了一個范圍。

一個文件夾 (目錄) 中可以包含多個文件夾,每個文件夾中不能有相同的文件名,但不同文件夾中的文件可以重名。

1. 命名空間

命名空間的定義使用關(guān)鍵字 namespace,后跟命名空間的名稱。

namespace namespace_name {
   // 代碼聲明
}

為了調(diào)用帶有命名空間的函數(shù)或變量,需要在前面加上命名空間的名稱。

namespace_name::yongqiang;  // yongqiang 可以是變量或函數(shù)
//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

// 第一個命名空間
namespace first_space {
	void yongqiang() {
		std::cout << "Inside first_space" << std::endl;
	}
}

// 第二個命名空間
namespace second_space {
	void yongqiang() {
		std::cout << "Inside second_space" << std::endl;
	}
}

int main()
{
	// 調(diào)用第一個命名空間中的函數(shù)
	first_space::yongqiang();

	// 調(diào)用第二個命名空間中的函數(shù)
	second_space::yongqiang();

	return 0;
}

Inside first_space
Inside second_space
請按任意鍵繼續(xù). . .

2. using 指令

using namespace 指令會告訴編譯器,后續(xù)的代碼將使用指定的命名空間中的名稱。

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

// 第一個命名空間
namespace first_space {
	void yongqiang() {
		cout << "Inside first_space" << endl;
	}
}

// 第二個命名空間
namespace second_space {
	void yongqiang() {
		cout << "Inside second_space" << endl;
	}
}

using namespace first_space;

int main()
{
	// 調(diào)用第一個命名空間中的函數(shù)
	first_space::yongqiang();

	// 調(diào)用第二個命名空間中的函數(shù)
	second_space::yongqiang();

	// 調(diào)用第一個命名空間中的函數(shù)
	yongqiang();

	return 0;
}

Inside first_space
Inside second_space
Inside first_space
請按任意鍵繼續(xù). . .

using 指令用來指定命名空間中的特定項目,

using std::cout;


使用 std 命名空間中的 cout 部分,在使用 cout 時就可以不用加上命名空間名稱作為前綴,但是 std 命名空間中的其他項目仍然需要加上命名空間名稱作為前綴。

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using std::cout;

int main()
{
	cout << "yongqiang cheng!" << std::endl;

	return 0;
}

yongqiang cheng!
請按任意鍵繼續(xù). . .

using 指令引入的名稱遵循正常的范圍規(guī)則。名稱從使用 using 指令開始是可見的,直到該范圍結(jié)束。在范圍以外定義的同名實體是隱藏的。

3. 不連續(xù)的命名空間

命名空間可以定義在幾個不同的部分中,因此命名空間是由幾個單獨定義的部分組成的。一個命名空間的各個組成部分可以分散在多個文件中。

如果命名空間中的某個組成部分需要請求定義在另一個文件中的名稱,則仍然需要聲明該名稱。下面的命名空間定義可以是定義一個新的命名空間,也可以是為已有的命名空間增加新的元素:

namespace namespace_name {
   // 代碼聲明
}

4. 嵌套的命名空間

命名空間可以嵌套,您可以在一個命名空間中定義另一個命名空間。

namespace namespace_name1 {
   // 代碼聲明
   namespace namespace_name2 {
      // 代碼聲明
   }
}

使用 :: 運算符來訪問嵌套的命名空間中的成員。

// 訪問 namespace_name2 中的成員
using namespace namespace_name1::namespace_name2;
 
// 訪問 namespace:name1 中的成員
using namespace namespace_name1;

如果使用的是 namespace_name1,那么在該范圍內(nèi) namespace_name2 中的元素也是可用的。

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;

// 第一個命名空間
namespace first_space {
	void yongqiang() {
		cout << "Inside first_space" << endl;
	}

	// 第二個命名空間
	namespace second_space {
		void yongqiang() {
			cout << "Inside second_space" << endl;
		}
	}
}

using namespace first_space::second_space;

int main()
{
	// 調(diào)用第一個命名空間中的函數(shù)
	first_space::yongqiang();

	// 調(diào)用第二個命名空間中的函數(shù)
	first_space::second_space::yongqiang();

	// 調(diào)用第二個命名空間中的函數(shù)
	yongqiang();

	return 0;
}

Inside first_space
Inside second_space
Inside second_space
請按任意鍵繼續(xù). . .

5. 命名空間內(nèi)變量、函數(shù)、全局變量的作用域

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

// 第一個命名空間
namespace first_space {
	int num = 99;

	// 第二個命名空間
	namespace second_space {
		int num = 11;
	}
}

// 全局變量
int num = 55;

int main()
{
	std::cout << "first_space::num = " << first_space::num << std::endl;
	std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
	std::cout << "num = " << num << std::endl;
	std::cout << "::num = " << ::num << std::endl;

	int num = 77;
	std::cout << "num = " << num << std::endl;
	std::cout << "::num = " << ::num << std::endl;

	return 0;
}

全局變量 num 表達為 ::num,當(dāng)有同名的局部變量時來區(qū)別兩者。

first_space::num = 99
first_space::second_space::num = 11
num = 55
::num = 55
num = 77
::num = 55
請按任意鍵繼續(xù). . .

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

// 第一個命名空間
namespace first_space {
	int num = 99;

	void yongqiang() {
		std::cout << "Inside first_space" << std::endl;
		std::cout << "num = " << num << std::endl;
	}

	// 第二個命名空間
	namespace second_space {
		int num = 11;

		void yongqiang() {
			std::cout << "Inside first_space" << std::endl;
			std::cout << "num = " << num << std::endl;
		}
	}
}

int main()
{
	std::cout << "first_space::num = " << first_space::num << std::endl;
	std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
	std::cout << "num = " << num << std::endl;
	std::cout << "::num = " << ::num << std::endl;

	return 0;
}

1>------ Build started: Project: hash_table, Configuration: Debug Win32 ------
1>  yongqiang.cpp
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(35): error C2065: 'num': undeclared identifier
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(36): error C2039: 'num': is not a member of '`global namespace''
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(36): error C2065: 'num': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

用 using 來告訴編譯器用到的是哪個命名空間內(nèi)的內(nèi)容。在 main() 中加 using namespace first_space; 或者 using namespace first_space::second_space;。但是不能同時使用,因為這樣也會導(dǎo)致編譯出錯,編譯器不知道要去使用哪個命名空間的變量和函數(shù)。

5.1 using namespace first_space;

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

// 第一個命名空間
namespace first_space {
	int num = 99;

	void yongqiang() {
		std::cout << "Inside first_space" << std::endl;
		std::cout << "num = " << num << std::endl;
	}

	// 第二個命名空間
	namespace second_space {
		int num = 11;

		void yongqiang() {
			std::cout << "Inside first_space" << std::endl;
			std::cout << "num = " << num << std::endl;
		}
	}
}

using namespace first_space;

int main()
{
	std::cout << "first_space::num = " << first_space::num << std::endl;
	std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
	std::cout << "num = " << num << std::endl;
	std::cout << "::num = " << ::num << std::endl;

	return 0;
}

first_space::num = 99
first_space::second_space::num = 11
num = 99
::num = 99
請按任意鍵繼續(xù). . .

5.2 using namespace first_space::second_space;

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

// 第一個命名空間
namespace first_space {
	int num = 99;

	void yongqiang() {
		std::cout << "Inside first_space" << std::endl;
		std::cout << "num = " << num << std::endl;
	}

	// 第二個命名空間
	namespace second_space {
		int num = 11;

		void yongqiang() {
			std::cout << "Inside first_space" << std::endl;
			std::cout << "num = " << num << std::endl;
		}
	}
}

using namespace first_space::second_space;

int main()
{
	std::cout << "first_space::num = " << first_space::num << std::endl;
	std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
	std::cout << "num = " << num << std::endl;
	std::cout << "::num = " << ::num << std::endl;

	return 0;
}

first_space::num = 99
first_space::second_space::num = 11
num = 11
::num = 11
請按任意鍵繼續(xù). . .

References

https://www.runoob.com/cplusplus/cpp-tutorial.html

總結(jié)

到此這篇關(guān)于C++語言中命名空間 (namespace)的文章就介紹到這了,更多相關(guān)C++命名空間 (namespace)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解C++之類和對象(2)

    詳解C++之類和對象(2)

    類是創(chuàng)建對象的模板,一個類可以創(chuàng)建多個對象,每個對象都是類類型的一個變量;創(chuàng)建對象的過程也叫類的實例化。每個對象都是類的一個具體實例(Instance),擁有類的成員變量和成員函數(shù)
    2021-11-11
  • C語言中 printf 函數(shù)輸出格式

    C語言中 printf 函數(shù)輸出格式

    這篇文章主要介紹了C語言中 printf 函數(shù)簡介,通過實例代碼給大家介紹Printf輸出格式的相關(guān)知識,需要的朋友可以參考下
    2021-08-08
  • C++ 模版雙向鏈表的實現(xiàn)詳解

    C++ 模版雙向鏈表的實現(xiàn)詳解

    本篇文章是對C++中的模版雙向鏈表進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言中switch語句基本用法實例

    C語言中switch語句基本用法實例

    switch的中文翻譯是開關(guān),顧名思義,開關(guān)的作用就是控制連通或者中斷,在C語言中switch語句的作用也是大同小異,下面這篇文章主要給大家介紹了關(guān)于C語言中switch語句基本用法的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Opencv使用鼠標(biāo)任意形狀的摳圖

    Opencv使用鼠標(biāo)任意形狀的摳圖

    這篇文章主要為大家詳細介紹了Opencv使用鼠標(biāo)任意形狀的摳圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C語言中輸入函數(shù)(scanf()、fgets()和gets())的區(qū)別詳解

    C語言中輸入函數(shù)(scanf()、fgets()和gets())的區(qū)別詳解

    這篇文章主要給大家介紹了關(guān)于C語言中三種輸入函數(shù)(scanf()、fgets()和gets())區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • 深入java線程池的使用詳解

    深入java線程池的使用詳解

    本篇文章是對java線程池的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言中的函數(shù)指針基礎(chǔ)學(xué)習(xí)教程

    C語言中的函數(shù)指針基礎(chǔ)學(xué)習(xí)教程

    這篇文章主要介紹了C語言中的函數(shù)指針基礎(chǔ)學(xué)習(xí)教程,包括函數(shù)指針作為參數(shù)來傳遞等重要知識,需要的朋友可以參考下
    2016-04-04
  • C++線程安全容器stack和queue的使用詳細介紹

    C++線程安全容器stack和queue的使用詳細介紹

    stack是一種容器適配器,專門用在具有后進先出操作的上下文環(huán)境中,其刪除只能從容器的一端進行 元素的插入與提取操作;隊列是一種容器適配器,專門用于在FIFO上下文(先進先出)中操作,其中從容器一端插入元素,另一端提取元素
    2022-08-08
  • C++中Stack(棧)的使用方法與基本操作詳解

    C++中Stack(棧)的使用方法與基本操作詳解

    Stack是一種常見的數(shù)據(jù)結(jié)構(gòu),常常被用來解決遞歸問題、括號匹配問題、函數(shù)調(diào)用棧等等。本文將介紹C++中stack的使用方法及基本操作,需要的可以參考一下
    2023-05-05

最新評論