Search code examples
c++scope-resolution

whats the difference between dot operator and scope resolution operator


I just wanted to know the difference between . operator and :: operator?


Solution

  • The former (dot, .) is used to access members of an object, the latter (double colon, ::) is used to access members of a namespace or a class.

    Consider the following setup.

    namespace ns {
        struct type
        {
            int var;
        };
    }
    

    In this case, to refer to the structure, which is a member of a namespace, you use ::. To access the variable in an object of type type, you use ..

    ns::type obj;
    obj.var = 1;