日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

你不知道的C++中namespace和using的用法實(shí)例_C 語(yǔ)言

作者:莫淺子 ? 更新時(shí)間: 2023-01-18 編程語(yǔ)言

引言

你是不是只認(rèn)為namespace 和 using 在C++中是基本的語(yǔ)法框架,但是卻不知道它們的真正用法,看完文章你會(huì)對(duì)using和namespace有一定了解,幫助你深入學(xué)習(xí)C++

一: 冒號(hào)作用域

:: 運(yùn)算符是一個(gè)作用域,如果::前面什么都沒有加 代表是全局作用域

也就是如果你輸入的數(shù)前加了:: 代表是全局變量?

代碼?

#include <iostream>
using namespace std;
 
int a = 100;
void test01()
{
	int a = 10;
	cout << a << endl;    //打印局部變量
	cout << ::a << endl;  //打印全局變量
}
int main()
{
	test01();
 
	return 0;
}

最后結(jié)果

10

100

二、名字控制

1 命令空間

namespace 本質(zhì)是作用域,可以更好的控制標(biāo)識(shí)符的作用域

命名空間 就可以存放 變量 函數(shù) 類 結(jié)構(gòu)體 ...

2 命令空間的使用

1)命令空間的定義 必須定義在全局范圍

2)命名空間下可以存放 變量 函數(shù) 結(jié)構(gòu)體 類

namespace A
{
	int a = 1;
 
	void fun()
	 {
		cout << "hello namespace" << endl;
	 }
	 void foo(int agr);
	 struct std   //結(jié)構(gòu)體
	 {};
	 class obj    //類
	 {};
	
}

3)命名空間可以重名 重名的命名空間相當(dāng)于做合并操作

namespace B
{
	int a = 10;
	int b = 20;
}
//命名空間可以重名
namespace B
{
	int c = 100;
}

4)命名空間可以嵌套命名空間

//命名空間可以嵌套
namespace C
{
	int a = 10;
	int b = 20;
	namespace D
	{
		int a = 100;
	}
}
void test02()
{
	cout << C::a << endl;
	cout << C::D::a << endl;
}

5)命名空間可以取別名, namespace newname = oldname;新名字與舊名字有同等效益

namespace NewA = A;

6)命名空間可以沒有名字 ,沒有名字相當(dāng)于給命名空間 內(nèi)的所有成員加上了static修飾

相當(dāng)于只能被當(dāng)前文件調(diào)用,屬于內(nèi)部鏈接屬性

namespace {
	int a = 10;
	void func() { cout << "hello namespace" << endl; }
 
}

7)命名空間中的函數(shù)可以先聲明,在外部定義,定義時(shí)需要加上命名空間作用域

namespace A
{
	 void foo(int agr);	
}
void A::foo(int arg)
{
	 cout << arg << endl;
}
void test03()
{
	A::foo(222);
}

總的代碼

#include <iostream>
using namespace std;
// 命令空間的定義 必須定義在全局范圍
// 命名空間下可以存放 變量 函數(shù) 結(jié)構(gòu)體 類
// 命名空間可以重名 重名的命名空間相當(dāng)于合并操作
// 命名空間可以嵌套命令空間
namespace A
{
	int a = 1;
 
	void fun()
	 {
		cout << "hello namespace" << endl;
	 }
	 void foo(int agr);
	 struct std   //結(jié)構(gòu)體
	 {};
	 class obj    //類
	 {};
	
}
//與A作用域下定義不一樣,這個(gè)在全局作用域下
void A::foo(int arg)
{
	 cout << arg << endl;
}
namespace NewA = A;
 
 //命名空間是可以取別名
 // namespace newname = oldname
 
namespace B
{
	int a = 10;
	int b = 20;
}
//命名空間可以重名
namespace B
{
	int c = 100;
}
//命名空間可以嵌套
namespace C
{
	int a = 10;
	int b = 20;
	namespace D
	{
		int a = 100;
	}
}
void test01()
{
	cout << A::a << endl;
	cout << B::a << endl;
	cout << B::b << endl;
	cout << B::c << endl;
	A::fun();        //表示A空間中fun函數(shù)調(diào)用
	
}
void test02()
{
	cout << C::a << endl;
	cout << C::D::a << endl;
}
void test03()
{
	A::foo(222);
}
 
namespace {
	int a = 10;
	void func() { cout << "hello namespace" << endl; }
 
}
 
int main()
{
	test01();
	test02();
	test03();
	return 0;
}

?三、?using的指令

1 using的聲明

usinng 的聲明可以使得指定標(biāo)識(shí)符可用

注意: 當(dāng)using聲明的標(biāo)識(shí)符和其他同名標(biāo)識(shí)符有作用域的沖突時(shí),會(huì)產(chǎn)生二義性

namespace nameA
{
	int a = 10;
	void foo()
	{
		cout << "Hello using" << endl;
	}
}
void test01()
{
	//注意當(dāng)using指定聲明標(biāo)識(shí)符和其他標(biāo)識(shí)符作用域有作用域的沖突時(shí),會(huì)產(chǎn)生二義性
	//int a = 100
	using nameA::a;
	using nameA::foo;
	cout << nameA::a << endl;
	cout << a << endl;
 
	foo();
}

2 using的編譯指令

void test02()
{
	int a = 1000;
	// using編譯指令使整個(gè)命名空間標(biāo)識(shí)符可用
	using namespace nameA;
	cout << a << endl; //結(jié)果為1000,就近原則
	foo();
}

總結(jié)代碼

#include <iostream>
using namespace std;
 
namespace nameA
{
	int a = 10;
	void foo()
	{
		cout << "Hello using" << endl;
	}
}
void test01()
{
	//注意當(dāng)using指定聲明標(biāo)識(shí)符和其他標(biāo)識(shí)符作用域有作用域的沖突時(shí),會(huì)產(chǎn)生二義性
	//int a = 100
	using nameA::a;
	using nameA::foo;
	cout << nameA::a << endl;
	cout << a << endl;
 
	foo();
}
void test02()
{
	int a = 1000;
	// using編譯指令使整個(gè)命名空間標(biāo)識(shí)符可用
	using namespace nameA;
	cout << a << endl; //結(jié)果為1000,就近原則
	foo();
}
int main()
{
	test01();
	test02();
}

?輸出

總結(jié)

原文鏈接:https://blog.csdn.net/qq_64691289/article/details/128339110

欄目分類
最近更新