what is the use of :: in c++?

what is the use of :: in c++?
"::" is the scope resolution operator, and it is used to define scope of namespaces and classes, outside of them.

For example, when we want to implement a class method outside of the class definition:

1
2
3
4
5
6
7
8
class MyClass {
  public:
    void SomeMethod();
};

void MyClass::SomeMethod() {
    // do something...
}


Or when we want to access static members of a class from outside:
1
2
3
4
5
6
7
8
9
10
11
class MyClass {
  public:
    static void StaticMethod() {
        // do something...
    }
}

int main()
{
    MyClass::StaticMethod(); // call the static function from MyClass...
}


Or when we want to use objects defined in the "std" namespace:

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "We used an object from 'std' namespace" << std::endl;

    return 0;
}
Registered users can post here. Sign in or register to post.