When there is a global variable and local variable with the same name, how will you access the global variable?



When there is a global variable and local variable with the same name, how will you access the globa..

Answer / Shiv Shakti Shankar

To access a global variable that has the same name as a local variable in C++, you must use the scope resolution operator (::). For example: `int x = 5; void someFunction() { int x = 10; std::cout << ::x << 'n'; }`

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Explain the difference between new() and malloc() in c++?

1 Answers  


Describe the advantage of an external iterator.

1 Answers  


What is the difference between an array and a list?

1 Answers  


Is c++ the hardest language?

1 Answers  


Find out the bug in this code,because of that this code will not compile....... #include <iostream> #include <new> #include <cstring> using namespace std; class balance { double cur_bal; char name[80]; public: balance(double n, char *s) { cur_bal = n; strcpy(name, s); } ~balance() { cout << "Destructing "; cout << name << "\n"; } void set(double n, char *s) { cur_bal = n; strcpy(name, s); } void get_bal(double &n, char *s) { n = cur_bal; strcpy(s, name); } }; int main() { balance *p; char s[80]; double n; int i; try { p = new balance [3]; // allocate entire array } catch (bad_alloc xa) { cout << "Allocation Failure\n"; return 1; }

2 Answers   Impetus,


What is function prototyping?

1 Answers  


what is polymorphism?

14 Answers   Accenture,


What is runtime errors c++?

1 Answers  


How does java differ from c and c++?

1 Answers  


What is a pdb file?

1 Answers  


What is a c++ object?

1 Answers  


What is multithreading and what is its use?Whats are multithreading techniques used in C++?

1 Answers  


Categories