What does :: do in C++

What does :: do in C++?

For example, std::string

Thanks to all that answer.
it can be used to access class function and namespaces

for example :
1
2
3
4
5
6
7
8
9

namespace abc {
    int variable;
};

int main(){
    cin >> abc::variable;
}


for std::string
it means that there is namespace called std and there is a class or a variable or whatever it is that is named string
but for std::string it is a class
:: is the scope resolution operator, and allows you to statically traverse scopes such as namespaces and classes in order to reference the identifier you want.

Class string is in namespace std, so you can access it in a variety of ways:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

namespace x = std;
namespace y
{
    namespace z = std;
}

int main()
{
    std::string a;
    ::std::string b;
    x::string c;
    y::z::string d;

    using std::string;
    string e;
}
Note that you should never write using namespace std; or anything similar.
Last edited on
:: is the scope resolution operator, which can be taken quite literally. It is thus used to resolve ambiguities pertaining to scope.

http://msdn.microsoft.com/en-us/library/b451xz31.aspx
Did you know you can use the scope resolution operator for global variables and functions as well?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
 
int i=6;
 
int main()
{
    int i=7;
 
    std::cout << ::i << ' ' << i << std::endl;
}
6 7


Helps make the code uglier, if you're into that sort of thing.
Last edited on
That's the 'resolution' part of it's name. Your new years resolution should similarly be to stop using global variables.
Thanks everyone.
Topic archived. No new replies allowed.