Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : what is :: for?

Tags:

c++

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?

like image 324
sivabudh Avatar asked Feb 17 '10 17:02

sivabudh


People also ask

What is :: operator in C?

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. C has a wide range of operators to perform various operations.

What does :: operator mean in C++?

In programming, an operator is a symbol that operates on a value or a variable. Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction. Operators in C++ can be classified into 6 types: Arithmetic Operators.

What is the name of :: operator?

It's called scope resolution operator. More information in Scope Resolution Operator (::) (PHP manual).


1 Answers

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 } 
like image 68
Skilldrick Avatar answered Sep 23 '22 19:09

Skilldrick