当前位置:网站首页>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
边栏推荐
- Jetpack Compose 教程
- 876. 链表的中间结点
- Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
- 添加命名空间声明
- Detailed explanation of issues related to SSL certificate renewal
- node_exporter部署
- 1008 Elevator(20 分)(PAT甲级)
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
- LeetCode FizzBuzz C#解答
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
猜你喜欢
Some thoughts on whether the judgment point is located in the contour
Hough transform Hough transform principle
[release] a tool for testing WebService and database connection - dbtest v1.0
A method of using tree LSTM reinforcement learning for connection sequence selection
Lm10 cosine wave homeopathic grid strategy
读写关闭的channel是啥后果?
自由小兵儿
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
Master the use of auto analyze in data warehouse
[uniapp] uniapp development app online Preview PDF file
随机推荐
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
Online text line fixed length fill tool
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
从实时应用角度谈通信总线仲裁机制和网络流控
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
测试工程师如何“攻城”(下)
876. 链表的中间结点
LeetCode FizzBuzz C#解答
Leetcode ransom letter C # answer
The 15th youth informatics competition in Shushan District in 2019
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
LeetCode第300场周赛(20220703)
Shell programming core technology "I"
Jetpack Compose 教程
Go microservice (II) - detailed introduction to protobuf
基于NCF的多模块协同实例
Nebula importer data import practice
牛客小白月赛7 E Applese的超能力
Shell 编程核心技术《四》