其他分享
首页 > 其他分享> > namespace的初级定义和使用(1)

namespace的初级定义和使用(1)

作者:互联网

#include <stdio.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	 */
int main(void)
{
	func1();
	
/* 	//method-1 usage of namespace 
	NameSpace1::func2();  */	//declare func2 which in NameSpace1
	
	func2(); 
	
	return 0;
}

有三种namespace的使用方式

三种都能用
第一种声明方式的话不用通过using去声明。

第二种的话通过using去声明一个函数。

第三种的话直接通过using整个namespace了。

我觉得有点像struct结构体和#include,如果让我选的话我就用第三种方式了,一次声明一劳永逸。

标签:func2,定义,void,namespace,初级,NameSpace1,usage,using
来源: https://blog.csdn.net/qq_40897531/article/details/120733697