c++ - 我应该怎么做才能访问变量 'a'?

标签 c++ namespaces

#include<iostream>
int a;    //1st a
void f()
{
   int a;    //2nd a

   {
        int a;   //3rd a
 // ::a refers to the global a here
 // simply a refers to the a inside this namespace


//What should i do to access and modify 2nd a here in this namespace

   }

}

我知道要访问全局名称我需要使用::a 但是如果我想访问的名称在函数内部并且我在该函数定义的 namespace 内我该怎么办?

最佳答案

没有从内部范围访问第二个a 的语法。您必须事先创建一个唯一的别名,例如:

void f()
{
     int a;
     {
          int& outer_a = a;
          int a;

关于c++ - 我应该怎么做才能访问变量 'a'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46880947/

相关文章:

c++ - 运算符优先级和评估顺序

naming-conventions - 如果我在不同的命名空间上有相同的类名怎么办?

python - Django 异常未注册的命名空间

c++ - 为什么将 char**(或任何 T**)转换为 void** 无效?

c++ - 使用 Visual Studio C++ 20 导入公共(public)模块文件夹的最佳方法

c++ - 避免到处都有静态常量变量

php - 如何在 phpStorm 中为整个 PHP 文件自动导入命名空间?

c++ - 将子类指针存储在父类指针的映射中

python - 为什么 token.py 的存在会导致内置 help() 中断?

php - 在 PhpDocumentor 2 中使用 @package 而不是命名空间