当前位置:网站首页>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
边栏推荐
猜你喜欢
线上数据库迁移的几种方法
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
[uniapp] uniapp development app online Preview PDF file
Summary and sorting of 8 pits of redis distributed lock
Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
Go微服务(二)——Protobuf详细入门
Online sql to excel (xls/xlsx) tool
Introduction to polyfit software
随机推荐
MySQL数据库基本操作-DDL | 黑马程序员
QT realizes interface sliding switching effect
Hough transform Hough transform principle
明明的随机数
Wireshark网络抓包
问下各位大佬有用过cdc直接mysql to clickhouse的么
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
Specify the character set to output
Go microservice (II) - detailed introduction to protobuf
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Generate XML elements
测试工程师如何“攻城”(下)
26. Delete the duplicate item C solution in the ordered array
Leetcode ransom letter C # answer
Hough Transform 霍夫变换原理
2019年蜀山区第十五届青少年信息学竞赛
Technologie de base de la programmation Shell IV
prometheus安装
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗