首页 > 编程语言 > 详细

c++命名空间namespace

时间:2019-02-10 10:06:57      阅读:173      评论:0      收藏:0      [点我收藏+]
namespace 变量作用域的作用空间,这样可以防止相同名称的变量被调用时带来的问题
#include "iostream" #include <string> using namespace std; namespace coachnamespace { string coachname = "陈培昌"; class mycoach { private: string name; int age; public: void myfunc(string _name, int _age) { name = _name; age = _age; cout << "大家好,我是" << name << "今年" << age; } }; } namespace studentnamespace { string name = "saintding"; namespace smspace { string fv1 = "散打"; string fv2 = "巴西柔术"; } namespace normalspace { string f1 = "买书"; string f2 = "吃大餐"; string f3 = "花钱"; } } void main() { using namespace coachnamespace; using namespace studentnamespace; studentnamespace::name; string _fv1= studentnamespace::smspace::fv1; cout << studentnamespace::name << "喜欢玩荤的" << _fv1 << endl; coachnamespace::mycoach mc; mc.myfunc("程劲",20); system("pause"); } 去掉.h的理由,早期引用头文件导致程序运行速度很慢

 

c++命名空间namespace

原文:https://www.cnblogs.com/saintdingspage/p/10358564.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!