#include <iostream>
#include <cstdlib>
namespace A {
int a = 10;
void get_value(){ std::cout << "a = " << a << std::endl; }
}
namespace B {
int b;
void get_value(){ std::cout << "b =" << b << std::endl; }
}
void set_B();
int main(){
using namespace A;
get_value();
set_B();
system("PAUSE");
return 0;
}
void set_B(){
using namespace B;
b = 15;
get_value(); // Why call to get_value() is ambiguous, error is not generated here ?
}
为什么在 set_B()
函数中调用 get_value()
没有歧义(A::get_value()
或 B::get_value()
) 因为两者都显示为 ::get_value()
在 set_B()
中。
最佳答案
using namespace A;
在 set_B
中不活跃,因为它只出现在 main
中。它仅限于它出现的范围:main
的主体。
关于c++ - 请帮助我使用 C++ 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326738/