当前位置:网站首页>Double colon function operator and namespace explanation
Double colon function operator and namespace explanation
2022-07-04 19:34:00 【Fat Da meow who can fly】
Reprinted address : Double colon function operator and namespace explanation , Are you sure you don't want to come and have a look ?_ Typing meow's blog -CSDN Blog
One 、 Double colon scope operator
Usually , If there are both local and global variables in the program , Local variables will get higher priority , It will mask global variables , But the double colon scope operator can solve the problem that local variables and global variables have the same name , The code is as follows :
int atk = 200;
void test01()
{
int atk = 100;
cout << " The attack power is : " << atk << endl;
// Double colon scope resolution :: Global scope
cout << " The overall attack power is : " << ::atk << endl;
}
The results are as follows
The attack power is : 100
The overall attack power is : 200
Please press any key to continue . . .
You can see , When we add :: when , Global variables will get higher priority .
Two 、c++ Namespace (namespace)
1. purpose
In the program , name ( Symbolic constant 、 Variable 、 function 、 structure 、 enumeration 、 Classes and objects, etc ) May conflict with each other ,namespace You can control the scope of each identifier , Make them avoid conflict .
2. Namespace uses syntax
2.1 Create a namespace
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
void test(){
cout << "A::a : " << A::a << endl;
cout << "B::a : " << B::a << endl;
}
In this way, the corresponding in each namespace can be printed a Value
2.2 Namespaces can only be defined as global variables
The following is a mistake
void test(){
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
cout << "A::a : " << A::a << endl;
cout << "B::a : " << B::a << endl;
}
2.3 Namespaces can be nested
The code is as follows
namespace A{
int a = 10;
namespace B{
int a = 20;
}
}
void test(){
cout << "A::a : " << A::a << endl;
cout << "A::B::a : " << A::B::a << endl;
}
2.4 Namespaces are open
Namespaces are open , You can add new members to the namespace at any time
namespace A{
int a = 10;
}
namespace A{
void func(){
cout << "hello namespace!" << endl;
}
}
void test(){
cout << "A::a : " << A::a << endl;
A::func();
}
2.5 No namespace
No namespace , It is equivalent to adding static, Make it an internal link , Can only be used in this document
namespace{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
cout << "a : " << a << endl;
func();
}
2.6 Namespaces can be aliased
We can alias namespaces at any time , Just like our parents nicknamed us
namespace veryLongName{
int a = 10;
void func(){ cout << "hello namespace" << endl; }
}
void test(){
namespace shortName = veryLongName;
cout << "veryLongName::a : " << shortName::a << endl;
veryLongName::func();
shortName::func();
}
3、 ... and 、using Declaration and using Compile instructions
1.using Declare the specified identifier
We can go through using Declaration to specify the identifier of a specific namespace
namespace zxy
{
int a = 10;
}
void test01()
{
int a = 20;
//using Statement Be careful to avoid ambiguity
// Yes using After declaration The following line of code shows what you will see later a Yes, it is zxy Under the
// however The compiler also has the principle of proximity
// Two senses
using zxy::a;
cout << a << endl;
}
This code will report an error , Because I wrote using After declaration , In the future, the compiler defaults a Yes, it is zxy Under the , Conflicts with the compiler's local variable proximity principle , So the program will report an error , When writing code , We should try to avoid ambiguity .
using namespace std;
namespace zxy
{
int a = 10;
}
void test01()
{
int a= 20;
using namespace zxy;
cout << a << endl;
}
There will be no error reporting in this way ,using namespace zxy; It only means opening zxy This room , Do not specify the identifier inside , The compiler follows the proximity principle .
2.using Compile instructions
Each use using It's like opening a room ,using Compilation instructions make identifiers available for the entire namespace .
namespace A{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test01(){
using namespace A;
cout << paramA << endl;
cout << paramB << endl;
funcA();
funcB();
// No ambiguity
int paramA = 30;
cout << paramA << endl;
}
As I said before , In this way, the compiler follows the principle of proximity , No ambiguity , Writing like this will cause problems
namespace B{
int paramA = 20;
int paramB = 30;
void funcA(){ cout << "hello funcA" << endl; }
void funcB(){ cout << "hello funcB" << endl; }
}
void test02(){
using namespace A;
using namespace B;
// Ambiguity arises , I don't know how to call A still B Of paramA
//cout << paramA << endl;
}
Used multiple times using Compile instructions , Opening multiple rooms at the same time will cause ambiguity
summary
That's what we're going to talk about today , This article only briefly introduces c++ Some basic knowledge of , I hope I can help you .
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Meow tapping the keyboard 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_59371851/article/details/124334915
边栏推荐
猜你喜欢

联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点

自由小兵儿
![[uniapp] uniapp development app online Preview PDF file](/img/11/d640338c626249057f7ad616b55c4f.png)
[uniapp] uniapp development app online Preview PDF file

升级智能开关,“零火版”、“单火”接线方式差异有多大?

The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology

从实时应用角度谈通信总线仲裁机制和网络流控

Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?

A method of using tree LSTM reinforcement learning for connection sequence selection

Stream流

FPGA时序约束分享01_四大步骤简述
随机推荐
Don't just learn Oracle and MySQL!
An example of multi module collaboration based on NCF
升级智能开关,“零火版”、“单火”接线方式差异有多大?
[release] a tool for testing WebService and database connection - dbtest v1.0
PolyFit软件介绍
添加命名空间声明
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
Allure of pytest visual test report
Online text line fixed length fill tool
1672. 最富有客户的资产总量
OpenCV的二值化处理函数threshold()详解
YOLOv5s-ShuffleNetV2
How test engineers "attack the city" (Part 2)
Shell 编程核心技术《二》
测试工程师如何“攻城”(下)
矩阵翻转(数组模拟)
Reflection (I)
Introduction to polyfit software
MySQL数据库基本操作-DDL | 黑马程序员
明明的随机数