当前位置:网站首页>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
边栏推荐
- Wireshark网络抓包
- 26. 删除有序数组中的重复项 C#解答
- How test engineers "attack the city" (Part I)
- The kth largest element in the array
- Hough transform Hough transform principle
- 1006 Sign In and Sign Out(25 分)(PAT甲级)
- Opencv functions and methods related to binary threshold processing are summarized for comparison and use
- 从实时应用角度谈通信总线仲裁机制和网络流控
- Functional interface
- Stream流
猜你喜欢
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
How to use async Awati asynchronous task processing instead of backgroundworker?
English语法_名词 - 使用
Online text line fixed length fill tool
关于判断点是否位于轮廓内的一点思考
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Summary and sorting of 8 pits of redis distributed lock
在线SQL转Excel(xls/xlsx)工具
随机推荐
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
牛客小白月赛7 谁是神箭手
Summary and sorting of 8 pits of redis distributed lock
BI技巧丨权限轴
性能优化之关键渲染路径
Is Guoyuan futures a regular platform? Is it safe to open an account in Guoyuan futures?
指定输出的字符集
欧拉函数
Shell 编程核心技术《三》
Mysql database basic operation -ddl | dark horse programmer
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
Shell programming core technology "three"
Nebula importer data import practice
读写关闭的channel是啥后果?
To sort out messy header files, I use include what you use
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
1005 Spell It Right(20 分)(PAT甲级)
“只跑一趟”,小区装维任务主动推荐探索
English语法_名词 - 使用
反射(一)