c++中namespace与using的解释

博客介绍了C++中namespace和using的相关知识。namespace可用于文件引用,为函数划定作用域,类似maya中的reference;using和namespace搭配使用,可省略空间域名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值