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 as output 1
, but I would like to have 5
, as in accessing the global x
variable.
我得到输出1,但我想有5,如访问全局x变量。
Is this possible?
这可能吗?
1 个解决方案
#1
26
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:
您应该使用:: x来访问本地范围内的全局变量。 operator ::是一元范围解析运算符。所以你的代码应该是:
#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++:
注意::: operator在C ++中有两个含义:
- Binary scope resolution operator.
- 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.
1.在课堂外定义你的功能。
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.
我们将代码组织成扩展名为.h的头文件和扩展名为.cpp的代码文件。在代码文件中定义函数时,我们使用:: Binary范围解析运算符。
For example,a Car.h file looks like:
例如,Car.h文件如下所示:
class Car
{
private:
int model;
int price;
public:
void drive();
void accelerate();
};
And Car.cpp would look like:
而Car.cpp看起来像:
void Car :: drive()
{
// Driving logic.
}
void Car :: accelerate()
{
// Logic for accelerating.
}
Here, as we can easily notice, ::
acts on two operands:
在这里,我们可以很容易地注意到,::对两个操作数进行操作:
- The class name
- 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.
因此,它基本上定义了函数的范围,即它通知编译器函数drive()属于Car类。
2. To resolve ambiguity between two functions with same template which are derived from different classes.
2.解决具有相同模板的两个函数之间的歧义,这些函数派生自不同的类。
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.
由于BMW类的派生函数都具有相同的模板,因此调用b.drive将导致编译错误。因此,要指定我们想要的drive(),我们使用::运算符。
3. To override the overridden function.
3.覆盖重写的功能。
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.
4.访问静态数据成员。
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;
}
#1
26
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:
您应该使用:: x来访问本地范围内的全局变量。 operator ::是一元范围解析运算符。所以你的代码应该是:
#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++:
注意::: operator在C ++中有两个含义:
- Binary scope resolution operator.
- 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.
1.在课堂外定义你的功能。
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.
我们将代码组织成扩展名为.h的头文件和扩展名为.cpp的代码文件。在代码文件中定义函数时,我们使用:: Binary范围解析运算符。
For example,a Car.h file looks like:
例如,Car.h文件如下所示:
class Car
{
private:
int model;
int price;
public:
void drive();
void accelerate();
};
And Car.cpp would look like:
而Car.cpp看起来像:
void Car :: drive()
{
// Driving logic.
}
void Car :: accelerate()
{
// Logic for accelerating.
}
Here, as we can easily notice, ::
acts on two operands:
在这里,我们可以很容易地注意到,::对两个操作数进行操作:
- The class name
- 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.
因此,它基本上定义了函数的范围,即它通知编译器函数drive()属于Car类。
2. To resolve ambiguity between two functions with same template which are derived from different classes.
2.解决具有相同模板的两个函数之间的歧义,这些函数派生自不同的类。
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.
由于BMW类的派生函数都具有相同的模板,因此调用b.drive将导致编译错误。因此,要指定我们想要的drive(),我们使用::运算符。
3. To override the overridden function.
3.覆盖重写的功能。
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.
4.访问静态数据成员。
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;
}