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也是侷限於當前文件,不再綴述。