using namespace Exercise; 不会导致 ival 的 redeclaration,
但是 using Exercise::ival; 反而会导致 redeclaration 是为什么;
如下面的代码:
#include <iostream>
using namespace std;
namespace Exercise
{
int ivar = 0;
double dvar = 0;
const int limit = 1000;
}
// using namespace Exercise;
using Exercise::ivar;
int ivar = 0;
命名空间就是解决命名冲突问题的,不过你那个
using Exercise::ivar;
相当于declare了一遍Exercise
里的ivar
变量,就是说冲突的不是ivar
和using Exercise::ivar
,而是你又declareusing Exercise::ivar
以下代码就没问题了,因为没有再次declare
using Exercise::ivar