whats the difference between dot operator and scope resolution operator

defiant picture defiant · May 24, 2010 · Viewed 12.1k times · Source

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

Answer

avakar picture avakar · May 24, 2010

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;