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

你不知道的C++中namespace和using的用法實(shí)例

 更新時間:2022年12月19日 11:30:03   作者:莫淺子  
在C++語言編寫的程序中,變量和函數(shù)等的作用范圍是有一定限制的,下面這篇文章主要給大家介紹了一些你不知道的C++中namespace和using的用法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

引言

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

一: 冒號作用域

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

也就是如果你輸入的數(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ù) 類 結(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ù)可以先聲明,在外部定義,定義時需要加上命名空間作用域

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作用域下定義不一樣,這個在全局作用域下
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)識符可用

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

namespace nameA
{
	int a = 10;
	void foo()
	{
		cout << "Hello using" << endl;
	}
}
void test01()
{
	//注意當(dāng)using指定聲明標(biāo)識符和其他標(biāo)識符作用域有作用域的沖突時,會產(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編譯指令使整個命名空間標(biāo)識符可用
	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)識符和其他標(biāo)識符作用域有作用域的沖突時,會產(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編譯指令使整個命名空間標(biāo)識符可用
	using namespace nameA;
	cout << a << endl; //結(jié)果為1000,就近原則
	foo();
}
int main()
{
	test01();
	test02();
}

 輸出

總結(jié)

到此這篇關(guān)于你不知道的C++中namespace和using用法的文章就介紹到這了,更多相關(guān)C++ namespace和using用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論