C++ : what is :: for?

前端 未结 5 983
名媛妹妹
名媛妹妹 2020-12-14 16:40

If you go to the accepted answer of this post

Could someone please elaborate on why he uses:

double temp = ::atof(num.c_str());

and

5条回答
  •  萌比男神i
    2020-12-14 17:01

    The :: operator is scope resolution operator.

    when used with class specifier as like A::a, it is to access the data memeber a of the class A. when used without any specifier, it access the global variable.

    It is used mainly in the following contests.

    1. To refer to the global variables.
    2. To refer to the static members of the class
    3. To avoid ambiguites, when a class inherits from multiple [ non-virtual base ] classes.
    4. With using directive, to use the base class functions in the derived class, when there is a function in base class with name same as that of the derived class, but for a different type.
    5. To access the functions defined in the global scope, when you have a function defined with the same signature, as in double temp = ::atof(num.c_str());
    6. To create objects of the nested classes.

提交回复
热议问题