C++基础教程二:命名空间

命名空间是为了区分同名变量或者函数的。创建的时候名字不能重复。

三种使用方法:

<1> using namespace name;

<2> name::variable name/function name (::作用域运算符)

<3> using name::member(指定开放某个成员)

代码:

#include <iostream>
using namespace std;
namespace spaceone {
    void putMsg() {
	cout << "spaceone msg."<<endl;
    }
}

namespace spacetwo {
    void putMsg() {
	cout << "spacetwo msg." << endl;
    }
}

using namespace spaceone;
int main()
{
    putMsg();
    spacetwo::putMsg();
    return 0;
}

 

打印结果:

spaceone msg.

spacetwo msg.

 

猜你喜欢

转载自blog.csdn.net/weixin_42565127/article/details/117693456