// 示例代码:namespace在C++中的使用
#include <iostream>
using namespace std;
// 定义一个命名空间
namespace first_space {
int value = 5;
void print() {
cout << "first_space: " << value << endl;
}
}
// 定义另一个命名空间
namespace second_space {
int value = 10;
void print() {
cout << "second_space: " << value << endl;
}
}
int main() {
// 使用命名空间中的成员
first_space::print(); // 输出 first_space: 5
second_space::print(); // 输出 second_space: 10
return 0;
}
first_space
和 second_space
,每个命名空间中都有一个名为 value
的整数和一个名为 print
的函数。main
函数中,我们通过 ::
操作符来访问不同命名空间中的成员,并调用相应的函数。希望这个示例能帮助你理解 namespace
在C++中的作用。
上一篇:c调用c++dll
下一篇:c++算法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站