Monday, July 27, 2009

What is meant " :: " in C or CPP?

:: in C or CPP means Scope Operator.





To understand it:-





Suppose the code is like this:-





#include%26lt;iostream.h%26gt;


int i=10;


void main()


{


int i=0;


cout%26lt;%26lt;i%26lt;%26lt;endl;


cout%26lt;%26lt;::i;


}


--------------------------------------...


By Compiling this program you can observe that the result of





cout%26lt;%26lt;i%26lt;%26lt;endl;





will be 0;





whereas the result of





cout%26lt;%26lt;::i%26lt;%26lt;endl;





will be 10;





now it is clear that when we use :: operator it accesses the global defined variable (here in example is ::i). and when we not use it then the program will take local variable i.

What is meant " :: " in C or CPP?
It is the scope resolution operator. You may like to check out this webpage http://www.cppreference.com/operator_pre...
Reply:"::" this is called Scope Resolution Operator...
Reply:it is called Scope Resolution Operator.
Reply:The scope resolution operator (::) in c++ is used to define the already declared member functions (in the header file with the .hpp extension)of the class. In the .cpp file one can define the normal functions or the member functions of the class. To differentiate from the normal functions with the member functions of the class one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where the ship is the class and the foo() is the member function in the ship. The other uses of the resolution operator is to resolve the scope of the variables if the same variable name is used for the global, local, and the data member of the class. If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is affected. If the resolution operator is placed in front of the variable name then the global variable is affected. If no resolution operator is placed then the local variable is affected.
Reply:That is what is known as a scope resolution operator. It tells you which namespad you are calling a function from thus eliminating conflicts because the same function might be defined in other libraries as well. For example std::cout tell you that you are calling the cout function from the std namespace


No comments:

Post a Comment