Skip to content

Commit

Permalink
增加几个域名空间用法
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee committed Apr 20, 2018
1 parent 8a26796 commit eaaeecd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 8 additions & 1 deletion chapter_9/file.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#include <iostream>

extern const int finger = 1; //const 声明的外部变量必需使用extern才生效
extern const int finger = 1; //const 声明的外部变量必需使用extern才生效

namespace pers {
void show_my_name(void)
{
std::cout << "liwei\n";
}
}
8 changes: 8 additions & 0 deletions chapter_9/header/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __FILE_H__
#define __FILE_H__

namespace pers {
void show_my_name(void);
}

#endif
19 changes: 16 additions & 3 deletions chapter_9/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <array>
#include <typeinfo>
#include "header/debug.h"
#include "header/file.h"

using namespace std;

Expand All @@ -29,9 +30,9 @@ int main(int agrs, char *argv[])
{
//globle_test();
//const_extern_test();
//namespace_test();
namespace_test();

debug_test();
//debug_test();
cin.get();
cin.get();
return 0;
Expand Down Expand Up @@ -81,13 +82,25 @@ namespace jill {
int a = 1;
}

//未命名相当于文件内部变量
namespace {
int b = 1;
}

void namespace_test(void)
{
using namespace jill;
show(a);
int a = 2;
show(a);
show(jill::a);

show(b); //没有名称的域名相当于文件内的全局变量

namespace john = jill; //域名重命名
show(john::a);

pers::show_my_name(); //使用其他文件的域名
}

//打印测试
Expand All @@ -99,4 +112,4 @@ void debug_test(void)
INFO("hahaha\n");
WARN("hahaha\n");
ERROR("hahaha\n");
}
}

0 comments on commit eaaeecd

Please sign in to comment.