Why would you write :: with no preceding namespace? (e.g. ::atof) [duplicate]
Asked Answered
F

5

32

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 not simply

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

Also, is it considered a good practice to use that syntax when you use "pure" global functions?

Fauch answered 17/2, 2010 at 17:5 Comment(7)
Also, how do you create a stackoverflow link such that when someone clicks on it, it goes directly to the answer (and not at the start of the page)?Fauch
Regarding direct-to-answer links, look in the lower left corner at the bottom of the answer for the "link" link.Crutcher
Like this: #1013071Lethia
There's a little bit of text that says link after the answer.Schonthal
There's a link beneath each answer saying "link"; that'll give you a permalink.Papua
@everyone : thank you! I never saw that "link" before!! lolFauch
Hmm... what about std::atof(). ::atof() means that you have included <stdlib.h> and want the global version, std::atof() means that you have included <cstdlib> (like a good C++ programmer) and want the version in the std namespace.Henigman
S
43

It says use the global version, not one declared in local scope. So if someone's declared an atof in your class, this'll be sure to use the global one.

Have a look at Wikipedia on this subject:

#include <iostream>

using namespace std;

int n = 12;   // A global variable

int main() {
    int n = 13;   // A local variable
    cout  << ::n << endl;  // Print the global variable: 12
    cout  << n   << endl;  // Print the local variable: 13
}
Schonthal answered 17/2, 2010 at 17:9 Comment(0)
N
19

:: is the scope resolution operator. Its use in this scenario, as a unary operator, is to ensure that the name (atof) is always looked up in the global scope -- this can be useful to prevent name hiding from interfering with the lookup.

Ninety answered 17/2, 2010 at 17:9 Comment(2)
That's funny, the Microsoft and Wikipedia examples are almost identical - I wonder which copied which?Schonthal
The most hilarious thing is that this should probably be std::atof() for C++ ;)Henigman
M
6

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.
Margarita answered 18/2, 2010 at 14:25 Comment(0)
B
1

::func() means that this function is not affiliated with any specific class. It is used when there exist many functions with the same name, and to avoid confusion between the one you want and specific member functions you precede the function name with the scope operator.


From C++ Primer, 4th edition, section 17.2.1:

"Names defined at global scope - names declared outside any class, function, or namespace - are defined inside the global namespace. The global namespace is implicitly declared and exists in every program. Each file that defines entities at global scope adds those names to the global namespace.

The scope operator can be used to refer to members of the global namespace. Because the global namespace is implicit, it does not have a name; the notation

::member_name

refers to a member of the global namespace."

Birnbaum answered 17/2, 2010 at 17:10 Comment(0)
S
1

Lets say you have two versions of a function f() one defined outside a namespace and one defined inside. Now lets say you have one more function g() in the same namespace. Now if you do f() inside g() it will call the one defined in the same namespace. But if you want to call the global version you need to do ::f()

Silverts answered 17/2, 2010 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.