mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-03 10:33:25 +08:00
update
This commit is contained in:
45
basic_content/using/using_global.cpp
Normal file
45
basic_content/using/using_global.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @file using_global.cpp
|
||||
* @brief using各种使用
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-07
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#define isNs1 1
|
||||
//#define isGlobal 2
|
||||
using namespace std;
|
||||
void func()
|
||||
{
|
||||
cout<<"::func"<<endl;
|
||||
}
|
||||
|
||||
namespace ns1 {
|
||||
void func()
|
||||
{
|
||||
cout<<"ns1::func"<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
namespace ns2 {
|
||||
#ifdef isNs1
|
||||
using ns1::func; /// ns1中的函数
|
||||
#elif isGlobal
|
||||
using ::func; /// 全局中的函数
|
||||
#else
|
||||
void func()
|
||||
{
|
||||
cout<<"other::func"<<endl;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
/**
|
||||
* 这就是为什么在c++中使用了cmath而不是math.h头文件
|
||||
*/
|
||||
ns2::func(); // 会根据当前环境定义宏的不同来调用不同命名空间下的func()函数
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user