c++中namespace與using的解釋

namespace:

文件的引用,爲函數劃定作用域,限制函數體內變量的作用域。相當於maya中的reference。

例如:

#include <maya/MMatrix.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MVector.h>



namespace testFile
{
	MString File;
	unsigned int numberSize;
	namespace other {
		MString File;
		unsigned int numberSize;
	
	}
}

void main(){
	namespace f = testFile; // 別名: 相當於python中的標籤(對象另一個名稱)
	f::other::File = MString("filepath"); // 設置

	cout << "the file path is " << f::other::File << endl;
	return;
}

using:

表示省略,和namespace搭配使用表示可以省略掉空間域名。

例如:

#pragma once
#include <maya/MMatrix.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MVector.h>



namespace testFile
{
	MString File;
	unsigned int numberSize;
	namespace other {
		MString File;
		unsigned int numberSize;
	
	}
}

void main(){
	using namespace testFile;
	other::File = MString("filepath"); // 設置

	cout << "the file path is " << other::File << endl;
	return;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章