你不知道的C++中namespace和using的用法實(shí)例
引言
你是不是只認(rèn)為namespace 和 using 在C++中是基本的語法框架,但是卻不知道它們的真正用法,看完文章你會(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é)
到此這篇關(guān)于你不知道的C++中namespace和using用法的文章就介紹到這了,更多相關(guān)C++ namespace和using用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談C++函數(shù)聲明后面加throw()的作用(必看)
下面小編就為大家?guī)硪黄獪\談C++函數(shù)聲明后面加throw()的作用(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
C語言實(shí)現(xiàn)linux網(wǎng)卡連接檢測的方法
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)linux網(wǎng)卡連接檢測的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
C++ namespace相關(guān)語法實(shí)例分析
這篇文章主要介紹了C++ namespace相關(guān)語法實(shí)例分析,對(duì)C++初學(xué)者有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例
這篇文章主要介紹了Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例,是很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
理解C++編程中的std::function函數(shù)封裝
這篇文章主要介紹了理解C++編程中的std::function函數(shù)封裝,std::function是C++11標(biāo)準(zhǔn)中的新特性,需要的朋友可以參考下2016-04-04
DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--浮點(diǎn)數(shù)的存儲(chǔ)格式
本文主要介紹DSP中浮點(diǎn)數(shù)的存儲(chǔ)格式,很值得學(xué)習(xí)一下,需要的朋友可以參考一下。2016-06-06
Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn)
這篇文章主要介紹了Linux UDP服務(wù)端和客戶端程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
C語言雙向鏈表實(shí)現(xiàn)根據(jù)使用頻率安排元素位置的功能實(shí)例代碼
這篇文章主要介紹了C語言雙向鏈表實(shí)現(xiàn)根據(jù)使用頻率安排元素位置的功能實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03

