C/C++ 當全域變數與區域變數名稱相同時,如何使用全域變數

直接試試看下面的程式碼就知道了。
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string data = "This ia a global variable.";
  5. int main()
  6. {
  7. string data = "This is a local variable.";
  8. cout << ::data << endl; //在變數名稱前面加上 :: 就是指全域變數
  9. cout << data << endl;
  10. system("pause");
  11. return 0;
  12. }

留言

這個網誌中的熱門文章

C# 井字遊戲