-->

Scope resolution operator

1 minute read

 Scope resolution operator (::) with example.

       The scope of the variable extends from the point its declaration till the                end of block containing the declaration.
      The global variable cannot be accessed within the inner block.
      Scope resolution operator (::) is used to cover a hidden variable.
Syntax:
:: variable-name
This operator allows to access the global version of variable.
Example:
# include (iostream.h)
int m = 10;
int main()
{
int m = 20;
{
cout <<” m =“<< m;
cout << endl << “::m=”<< ::m;
return 0;
}
Output:
20
10
When we use scope resolution operator, it refers to the global variable.
A major application of the scope resolution operator is in the classes to identity  the class to which member function belongs.

    Memory management operator 

    ·    C++ provides a two unary operators named new and delete that                    performs the management of memory.
    ·    The task of these operators is to allocate the memory and free memory.
    ·     An object can be created by using new, and destroyed by using delete          operator.
    ·      These operators are known as free store operators.
    ·      The new operator is used to create the objects of any type.
    Syntax:
·      pointer-variable = new data-type;
    ·   When a data object is no longer needed, it is destroyed to release the              memory space for further use.
    Syntax:
·      delete pointer-variable;

How do you dynamically allocate memory for an integer array of 10 bytes?

       Int  *p;
       p=new int[10];