命名空間

test.h中:

 

namespace tt
{
  void f();
}

 

test.cpp中:

#include "test.h"    //可不包含test.h,編譯階段是將同一個命名空間編譯到一起的。

 

namespace tt

  void f()
 {

 

 }
}

 

main.cpp中:

#include "test.h"   //必須包含,因爲找不到命名空間tt。

void main()
{
 tt::f();
}

 

 

 

 

static是侷限於當前文件。

 

test.h中:

 

namespace tt
{
  static void f();
}

 

test.cpp中:

 

namespace tt

  static void f()
 {

 

 }
}

 

main.cpp中:

 

#include "test.h"   //error C2129: 靜態函數“void tt::f(void)”已聲明但未定義

void main()
{
      tt::f(); //error C2129: 靜態函數“void tt::f(void)”已聲明但未定義

}

 

而如果test.h中 改成:

namespace tt
{
  static void f()

{}
}

則OK。

但如果test.cpp中包含test.h,則會出現重定義。因爲將test.h展開到test.cpp中時,函數f()定義了兩個主體。

 

 

另外:inline也是侷限於當前文件,不再綴述。

發佈了26 篇原創文章 · 獲贊 39 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章