[C++] 用Xcode来写C++程序[6] Name visibility
生活随笔
收集整理的這篇文章主要介紹了
[C++] 用Xcode来写C++程序[6] Name visibility
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用Xcode來寫C++程序[6] Name visibility
?
此小結包括了命名空間的一些使用細節
?
命名空間
#include <iostream> using namespace std;namespace foo {// 函數int value() {return 5;} }namespace bar {// 常量const double pi = 3.1416;// 函數double value() {return 2*pi;} }int main () {cout << foo::value() << '\n';cout << bar::value() << '\n';cout << bar::pi << '\n';return 0; }打印結果
5 6.2832 3.1416 Program ended with exit code: 0?
?
?
使用命名空間
#include <iostream> using namespace std;namespace first {int x = 5;int y = 10; }namespace second {double x = 3.1416;double y = 2.7183; }int main () {// 聲明使用命名空間中的某個元素using first::x;using second::y;cout << x << '\n';cout << y << '\n';// 直接使用命名空間中的某個元素cout << first::y << '\n';cout << second::x << '\n';return 0; }打印結果
5 2.7183 10 3.1416 Program ended with exit code: 0?
#include <iostream> using namespace std;namespace first {int x = 5;int y = 10; }namespace second {double x = 3.1416;double y = 2.7183; }int main () {// 聲明使用命名空間first中的元素using namespace first;cout << x << '\n';cout << y << '\n';// 使用命名空間second中的元素cout << second::x << '\n';cout << second::y << '\n';return 0; }打印結果
5 2.7183 10 3.1416 Program ended with exit code: 0?
#include <iostream> using namespace std;namespace first {int x = 5; }namespace second {double x = 3.1416; }int main () {// 使用命名空間first {using namespace first;cout << x << '\n';}// 使用命名空間second {using namespace second;cout << x << '\n';}return 0; }打印結果
5 3.1416 Program ended with exit code: 0?
總結
以上是生活随笔為你收集整理的[C++] 用Xcode来写C++程序[6] Name visibility的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动处理键盘事件的第三方库 IQKeyb
- 下一篇: SCU 3133(博弈)