Code About Us Tutorial


  Statements & Expression
    The while Statement
    The do ... while Statement
    The for Statement
    The break Statement
    The continue Statement
    The goto Statement
    The switch Statement
    Keywords
    Scope Resolution (::)
    Arithmatic Operators


   
 

Home > Statements & Expression > Scope Resolution (::)

November 30, 2009 9:14 am

   

Scope Resolution (::)

In this section of statements and expressions, we will take a look at the scope resolution operator (or double colons "::") that is new to C++. What it does is that it allows you to specify which scope to use rather than using the default scope (the local scope).

To use the global scope, you need to prefix the variable name with the scope resolution operator (::) and to use the local scope, you do not need to do anything to the variable name since by default, the scope is the local scope.

 
 
int amount = 123;                // global variable
void main()
{
    int amount = 456;            // local variable
    cout << ::amount << endl;    // Print the global variable (123)
    cout << amount << endl;      // Print the local variable (456)
}

Looking at the above code, there are two instances of the variable amount. One being in the global scope which contains the value 123 and the other in the local scope of the function main() which is initiated with a value of 456. The two colons tell the compiler to use the global amount instead of the local one.Please Rate this Code:


    Comments for: Scope Resolution (::)
Annonymouse User says:
V.good. Didn\'t know that!

Alex [alex.bunting@ntlworld.com] Posted: 3 times. says:
me either, thanx for that (now all i gotta do is work out how to use things out of int main()

Alex [alex.bunting@ntlworld.com] Posted: 3 times. says:
me either, thanx for that (now all i gotta do is work out how to use things out of int main() says:
Really good tutorial. Short but usefull.

Alex [alex.bunting@ntlworld.com] Posted: 3 times. says:
me either, thanx for that (now all i gotta do is work out how to use things out of int main() says:
Really good tutorial. Short but usefull. says:
good tutorial keeps things simple

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Yes, I didnt think this tutorial had alot to offer so I minimized the gibberish and got straight to the point.

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Yes, I didnt think this tutorial had alot to offer so I minimized the gibberish and got straight to the point. says:
teach some gibberish

kahl [tikal_@hotmail.com] Posted: 5 times. says:
how nice could you be to do this for free! THANK YOU THANK YOU. (got an exam in 2 days! lol)




Add Comment:
Name:
Email:


 «4» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.