当前位置:网站首页>1、 Header file, output format,::, namespace
1、 Header file, output format,::, namespace
2022-07-26 17:53:00 【Rock magnon】
List of articles
- 1、C++ The header file
- 2、 Standard output format
- 3、:: scope resolution
- 4、 Namespace
- 1、 establish
- 2、 Nesting of namespace
- 3、 Namespaces are open , You can join new members at any time , And will retain the original members
- 4、 Declaration and implementation are separate , Declare in namespace , Implement elsewhere
- 5、 Nameless namespace , Namespace alias
- 6、using Declaration and compilation
1、C++ The header file
C++ Header files do not use extensions , Use the library name directly . In order to distinguish between C Language and C++ Different language libraries , The library name will be prefixed with letters c To distinguish between .
for example :
- C Language :math.h
- C++:cmath
2、 Standard output format
cout<<" Output content "<<endl;
This is a C++ Standard output format ,\n And endl The same effect , But it is recommended endl.
3、:: scope resolution
#include<iostream>
using namespace std;
int a = 10;
int main(){
int a = 20;
// When local variables and global variables conflict , Local variables take precedence over global variables , Global variables are masked
cout << " local variable a=" << a << endl;
//:: Represents the use of global variables , Open access to global variables
cout << " Global variables a=" << ::a << endl;
return 0;
}
local variable a=20
Global variables a=10
4、 Namespace
1、 establish
#include<iostream>
using namespace std;
// Create a namespace
namespace A
{
int a = 10;
} // namespace A
namespace B
{
int a =20;
} // namespace B
int main(){
cout << "namespace A:a=" << A::a << endl;
cout << "namespace B:a=" << B::a << endl;
}
Be careful , Namespaces can only be defined globally , The following code example is wrong :
#include<iostream>
using namespace std;
int main(){
namespace A
{
int a = 10;
} // namespace A
cout << "namespace A:a=" << A::a << endl;
}
Error message :
hello.cpp: In function ‘int main()’:
hello.cpp:5:5: error: ‘namespace’ definition is not allowed here
5 | namespace A
| ^~~~~~~~~
hello.cpp:10:33: error: ‘A’ has not been declared
10 | cout << "namespace A:a=" << A::a << endl;
| ^
2、 Nesting of namespace
#include<iostream>
using namespace std;
namespace A
{
int a = 10;
namespace B
{
int a = 20;
} // namespace B
} // namespace A
int main(){
cout << "namespace A:a=" << A::a << endl;
cout << "namespace A::B::a=" << A::B::a << endl;
}
3、 Namespaces are open , You can join new members at any time , And will retain the original members
#include<iostream>
using namespace std;
namespace A
{
int a = 10;
namespace B
{
int a = 20;
} // namespace B
} // namespace A
namespace A
{
int b = 200;
} // namespace A
int main(){
cout << "namespace A:b =" << A::b << endl;
cout << "namespace A::B::a=" << A::B::a << endl;
}
4、 Declaration and implementation are separate , Declare in namespace , Implement elsewhere
#include<iostream>
using namespace std;
namespace func_space
{
void func();
} // namespace func_space
void func_space::func(){
cout <<"declaration and function are sparate!" << endl;
}
int main(){
func_space::func();
}
5、 Nameless namespace , Namespace alias
#include<iostream>
using namespace std;
namespace func_space
{
void func();
} // namespace func_space
/* Nameless namespace , It is equivalent to adding static, Can only be accessed within this file */
namespace
{
int c = 1000;
} // namespace
// Alias namespace , It is equivalent to that the same space has different names
namespace func_space_copy = func_space;
void func_space_copy::func(){
cout <<"declaration and function are sparate!" << endl;
cout << "c=" << c << endl;
}
int main(){
func_space::func();
}
6、using Declaration and compilation
compile :using namespace A; Statement :using A::a;
#include<iostream>
using namespace std;
namespace A
{
int a = 10;
int b = 20;
} // namespace A
void test_01(){
// Include relational output directly through namespaces
cout << A::a << endl;
// Output through namespace declaration
using namespace A;
//could visit b
cout << b << endl;
}
void test_02(){
using A::a;
// You can visit a
cout << a << endl;
// No access b, Application error :error: ‘b’ was not declared in this scope; did you mean ‘A::b’?
//cout << b << endl;
}
void test_03(){
// Only the namespace is declared here A, Didn't say use a, So the output is 20
using namespace A;//using Compile instructions , bring A All identifiers in are available
cout << a << endl;// out a = 10
int a = 20;// There is no conflict
cout << a << endl;// out a = 20
}
void test_04(){
// Conflict of the same name , Will report a mistake
//error: ‘int a’ conflicts with a previous declaration
using A::a;
//int a = 20;
}
int main(){
test_03();
}
边栏推荐
- 如何通过学会提问,成为更加优秀的数据科学家
- JS 递归 斐波那契数列 深克隆
- [training day3] section
- JS function scope variables declare that the variables that promote the scope chain without VaR are global variables
- 我们被一个 kong 的性能 bug 折腾了一个通宵
- Comparison between agile development and Devops
- 浅析接口测试
- 兆骑科创海外高层次人才引进平台,创业赛事活动路演
- Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming
- Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market
猜你喜欢

How to set IP for layer 2 management switches

【元宇宙欧米说】剖析 Web3 风险挑战,构筑 Web3 生态安全

(25) top level menu of blender source code analysis blender menu

Kudu design tablet

236. 二叉树的最近公共祖先

Several ways to resolve hash conflicts

树形dp问题
2.1.2 synchronization always fails

Analysis of interface testing

kaggle猫狗数据集开源——用于经典CNN分类实战
随机推荐
【机器学习】Mean Shift原理及代码
【模板】线段树 1
the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
AI遮天传 DL-多层感知机
徽商期货网上开户安全吗?开户办理流程是怎样的?
天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞
来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!...
点击劫持攻击
Definition of graph traversal and depth first search and breadth first search (I)
(25)Blender源码分析之顶层菜单Blender菜单
就这一次!详细聊聊分布式系统的那些技术方案
【云原生之kubernetes实战】安装kubeopertor教程
kudu设计-tablet
Just this time! Talk about the technical solutions of distributed system in detail
跨站点请求伪造(CSRF)
6-19 vulnerability exploitation -nsf to obtain the target password file
Asemi rectifier bridge kbpc3510, kbpc3510 package, kbpc3510 application
重磅公布!ICML2022奖项:15篇杰出论文,复旦、厦大、上交大研究入选
【集训Day2】Sculpture