怎么用
例子:func3
在file2
的NameSpace2
里面
把func3声明在TestNamespace.h
里面
在file1里面使用的时候,需要using
关键字声明NameSpace2
file1 (namespace_def_used.cpp)
#include <stdio.h>
#include "TestNamespace.h"
//define a name space
namespace NameSpace1
{
void func2(void)
{
printf("this is function 2 in namespace !\n");
}
}
void func1(void)
{
printf("this is default function One!\n");
}
/* //method-2 usage of namespace
using NameSpace1::func2; //declare func2 which in NameSpace1 */
/* //method-3 usage of namespace
using namespace NameSpace1; //declare func2 which in NameSpace1 */
using namespace NameSpace2; //delcare NameSpace2
int main(void)
{
func1();
/* //method-1 usage of namespace
NameSpace1::func2(); */ //declare func2 which in NameSpace1
/* func2(); */
func3();
return 0;
}
file2 (namespace_def_used2.cpp)
#include <stdio.h>
namespace NameSpace2
{
void func3(void)
{
printf("func3 in the other file!\n");
}
}
file3 (TestNameSpace.h)
//declare func3 in the other file
namespace NameSpace2{void func3(void);};