关于未命名名称空间

// file1.cpp
#include <iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
int main()
{
 cout << x << endl;
 {
  int x = 4;
  cout << x << endl;
  cout << y << endl;        //这里y为什么输出0?
 }
 other();
 another();
 return 0;
}
void other()
{
 int y = 1;
 cout << "Other: " << x << ", " << y << endl;
}

// file 2.cpp
#include <iostream>
using namespace std;
extern int x;
namespace
{
int y = -4;
}
void another()
{
cout << "another(): " << x << ", " << y << endl;
}


xungeer29
浏览 1119回答 1
1回答

慕移动6052691

y为全局变量,默认初始值为0
打开App,查看更多内容
随时随地看视频慕课网APP