当前位置:网站首页>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
边栏推荐
- Shell 编程核心技术《二》
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- 876. Intermediate node of linked list
- 一文掌握数仓中auto analyze的使用
- Bi skills - permission axis
- Unity editor extends C to traverse all pictures in folders and subdirectories
- Technology sharing | interface testing value and system
- The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
- Go microservice (II) - detailed introduction to protobuf
- How test engineers "attack the city" (Part 2)
猜你喜欢

LeetCode第300场周赛(20220703)

更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?

Master the use of auto analyze in data warehouse

Online text line fixed length fill tool

Mysql database basic operation -ddl | dark horse programmer

Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading

OpenCV的二值化处理函数threshold()详解

Don't just learn Oracle and MySQL!

"Only one trip", active recommendation and exploration of community installation and maintenance tasks

FPGA时序约束分享01_四大步骤简述
随机推荐
Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
The 300th weekly match of leetcode (20220703)
2021 Hefei informatics competition primary school group
Oracle with as ORA-00903: invalid table name 多表报错
MySQL数据库基本操作-DDL | 黑马程序员
Unity adds a function case similar to editor extension to its script, the use of ContextMenu
An example of multi module collaboration based on NCF
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
Leetcode ransom letter C # answer
偏移量函数及开窗函数
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
English语法_名词 - 使用
Functional interface
测试工程师如何“攻城”(上)
添加命名空间声明
1002. A+B for Polynomials (25)(PAT甲级)
反射(一)
整理混乱的头文件,我用include what you use
Reflection (I)
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理