gpt4 book ai didi

c++ - 请帮助我使用 C++ 命名空间

转载 作者:行者123 更新时间:2023-11-30 02:44:26 26 4
gpt4 key购买 nike

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com