280 views

1 Answer

0 votes
0 votes

1. To access a global variable when there is a local variable with the same name

int count = 0;

int main(void) {
  int count = 0;
  ::count = 1;  // set global count to 1
  count = 2;    // set local count to 2
  return 0;
}

The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.

2. You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.

#include <iostream>
using namespace std;

class X
{
public:
      static int count;
};
int X::count = 10;                // define static data member

int main ()
{
      int X = 0;                  // hides class type X
      cout << X::count << endl;   // use static member of class X
}

the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.

1. https://www.geeksforgeeks.org/scope-resolution-operator-in-c/

2. https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr175.htm

Related questions

0 votes
0 votes
2 answers
1
iarnav asked May 27, 2017
465 views
What is meant by precedence rule for evaluation of expressions?
0 votes
0 votes
1 answer
2
iarnav asked May 27, 2017
234 views
What are structured languages and what are their major features?
0 votes
0 votes
1 answer
3
iarnav asked May 27, 2017
260 views
Consider a pointer declaration int i=10,*p; p=&x;Is p – – ; a valid statement, justify? explain in as simple way as possible.
0 votes
0 votes
1 answer
4
iarnav asked May 27, 2017
234 views
What is the main difference between array of pointers and pointer to an array? please Explain with the help of a program/example