如何在C ++中访问本地范围内的全局变量 [英] How to access a global variable within a local scope in C++

查看:173
本文介绍了如何在C ++中访问本地范围内的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

  #include< iostream& 
using namespace std;
int x = 5
int main()
{
int x = 1;
cout<< 变量x:< x<< endl;
}

我得到o / p为1.我想o / p为5即我想访问全局变量。请帮帮我。我是C ++的新手。

解决方案

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

  #include< iostream& 
using namespace std;
int x = 5;
int main()
{
int x = 1;
cout<< 变量x:< :: x< endl;
}

注意: :: operator在C ++中有两个含义:


  1. 二进制范围解析操作符。


几乎在整个编码时间内,您将使用二进制范围解析运算符。所以虽然这个问题的答案是一元范围解析算子;



使用二进制范围解析运算符的情况:



1。



我们使用 .h 扩展名将代码整理到头文件中, em> .cpp 扩展名。在代码文件中定义我们的函数时,我们使用 :: 二进制范围解析运算符。



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

  class Car 
{
private:
int model;
int price;

public:
void drive();
void accelerate();
};

并且 Car.cpp 会像:

  void Car :: drive()
{
//驱动逻辑。
}
void Car :: accelerate()
{
//加速逻辑。
}

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


  1. 类名

  2. name

因此,它本质上定义了函数的范围,即它通知编译器函数 drive > $ b

2。



请考虑以下代码:

  #include< iostream> 
using namespace std;
class Vehicle
{
public:
void drive()
{
cout< 我正在驾驶一辆Vehicle.\\\
;
}
};
class Car
{
public:
void drive()
{
cout< 我驾驶一辆Car.\\\
;
}
};
class BMW:public Car,public Vehicle
{
// BMW具体功能。
};
int main(int arc,char ** argv)
{
BMW b;
b.drive(); //这将给编译错误,因为调用是不明确的。
b.Car::drive(); //将调用Car的驱动方法。
b.Vehicle :: drive(); //将调用Vehicle的驱动方法。
}

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






3。



二进制范围解析操作符有助于调用基类的函数,在派生类中使用派生类的对象。请参阅下面的代码:

  #include< iostream& 
using namespace std;
class Car
{
public:
void drive()
{
cout< 我正在驾驶Car.\\\
;
}
};
class BMW:public Car
{
public:
void drive()
{
cout< 我正在驾驶BMW \\\
;
}
};
int main(int argc,char ** argv)
{
BMW b;
b.drive(); //将调用宝马的驱动功能。
b.Car::drive(); //将调用Car的驱动函数。
}






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 :: car_variable<< '\\\
';
return 0;
}


This is my code

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

I get the o/p as 1. I want o/p as 5 i.e. I want to access global variable. Please help me. I am new to C++.

解决方案

You should be using ::x in order to access global variable in local scope. The operator :: is unary scope resolution operator. So your code should be:

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

Note: :: operator has two meanings in C++:

  1. Binary scope resolution operator.
  2. Unary Scope resolution operator.

Almost for your entire coding hours, you would be using the Binary scope resolution operator. So although the answer to this question is unary scope resolution operator; just for the sake of future reference, I enlist some typical use cases of the Binary scope resolution operator.

Use cases of the Binary scope resolution operator:

1. To define your functions outside the class.

We organize our code into header files with .h extension and code files with .cpp extension. While defining our functions in the code files, we use the :: Binary scope resolution operator.

For example,a Car.h file looks like:

class Car
{
    private:
        int model;
        int price;

    public:
        void drive();
        void accelerate();
};

And Car.cpp would look like:

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

Here, as we can easily notice, :: acts on two operands:

  1. The class name
  2. The function name

Hence, it essentially defines the scope of the function i.e. it informs the compiler that the function drive() belongs to the class Car.


2. To resolve ambiguity between two functions with same template which are derived from different classes.

Consider the following code:

#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.
}

As both the derived functions of the class BMW have the same template, the call b.drive will result into a compilation error. Hence, to specify which drive() we want, we use the :: operator.


3. To override the overridden function.

Binary scope resolution operator helps to call the function of the base class which is overridden in a derived class using the derived class's object. See the code below:

#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. To access static data members.

As we know, static data members are shared per class basis by the objects of that class. Hence, we should not (although we can) use objects to scope the static variables. See the following code:

#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 ++中访问本地范围内的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆