【问题标题】:How to access a global variable within a local scope?如何访问局部范围内的全局变量?
【发布时间】:2013-12-25 20:38:00
【问题描述】:

这是我的代码

#include <iostream>
using namespace std;
int x = 5;
int main()
{
    int x = 1;
    cout << "The variable x: " << x << endl;
}   

我得到了1 的输出,但我想要5,就像访问全局x 变量一样。

这可能吗?

【问题讨论】:

    标签: c++ global-variables local


    【解决方案1】:

    您应该使用::x 来访问本地范围内的全局变量。运算符:: 是一元范围解析运算符。所以你的代码应该是:

       #include <iostream>
       using namespace std;
       int x = 5;
       int main()
       {
           int x = 1;
           cout << "The variable x: " << ::x << endl;
       }   
    

    注意::: 运算符在 C++ 中有两种含义:

    1. 二进制范围解析运算符。
    2. 一元范围解析运算符。

    几乎在您的整个编码时间中,您都将使用二进制范围解析运算符。因此,尽管这个问题的答案是一元范围解析运算符;只是为了将来参考,我列出了二进制范围解析运算符的一些典型用例。

    二进制范围解析运算符的用例:

    1.在类之外定义你的函数。

    我们将代码组织成扩展名为 .h 的头文件和扩展名为 .cpp 的代码文件。在代码文件中定义函数时,我们使用:: 二进制范围解析运算符。

    例如,Car.h 文件如下所示:

    class Car
    {
        private:
            int model;
            int price;
    
        public:
            void drive();
            void accelerate();
    };
    

    Car.cpp 看起来像:

    void Car :: drive()
    {
        // Driving logic.
    }
    void Car :: accelerate()
    {
        // Logic for accelerating.
    }
    

    在这里,我们很容易注意到,:: 作用于两个操作数:

    1. 类名
    2. 函数名

    因此,它本质上定义了函数的范围,即它通知编译器函数drive() 属于汽车。


    2。解决从不同类派生的具有相同模板的两个函数之间的歧义。

    考虑以下代码:

    #include <iostream>
    using namespace std;
    class Vehicle
    {
        public:
        void drive()
        {
            cout << "I am driving a Vehicle.\n";
        }
    };
    class Car
    {
        public:
        void drive()
        {
            cout << "I am driving a Car.\n";
        }
    };
    class BMW : public Car, public Vehicle
    {
        // BMW specific functions.
    };
    int main(int arc, char **argv)
    {
        BMW b;
        b.drive();  // This will give compile error as the call is ambiguous.
        b.Car::drive();  // Will call Car's drive method.  
        b.Vehicle::drive();  // Will call Vehicle's drive method.
    }
    

    由于 BMW 类的两个派生函数具有相同的模板,因此调用 b.drive 将导致编译错误。因此,要指定我们想要的 drive(),我们使用 :: 运算符。


    3.覆盖被覆盖的函数。

    二进制范围解析运算符有助于调用基类的函数,该函数在使用派生类的对象的派生类中被覆盖。请看下面的代码:

    #include <iostream>
    using namespace std;
    class Car
    {
        public:
        void drive()
        {
            cout << "I am driving Car.\n";
        }
    };
    class BMW : public Car
    {
        public:
        void drive()
        {
            cout << "I am driving BMW\n";
        }
    };
    int main(int argc, char** argv)
    {
        BMW b;
        b.drive(); // Will call BMW's drive function.
        b.Car::drive(); // Will call Car's drive function.
    }
    

    4.访问静态数据成员。

    正如我们所知,静态数据成员在每个类的基础上由该类的对象共享。因此,我们不应该(尽管我们可以)使用对象来限定静态变量。见以下代码:

    #include <iostream>
    using namespace std;
    class Car
    {
        public:
        static int car_variable;
    };
    int Car :: car_variable;
    int main(int argc, char** argv)
    {
        Car :: car_variable = 10;
        cout << "Car variable: " << Car :: car_variable << '\n';
        return 0;
    }
    

    【讨论】:

    • 我不会说这是“纯粹的基础”。你可以在不了解命名空间的情况下做很多 C++。
    • 再想一想,我同意。编辑答案。
    • 我认为一元 :: 和二元 :: 是一回事,只是全局范围没有名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2016-11-13
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多