Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of a leading "::" in a C++ method call

Tags:

c++

boost

I've been using the Boost libraries, and in Boost.Exception, I've noticed code like the following:

#define BOOST_THROW_EXCEPTION(x) ::boost::throw_exception(x) 

Just out of curiosity: what is the purpose of the leading :: before boost::throw_exception(x)?

like image 278
bhood Avatar asked Jan 12 '10 23:01

bhood


People also ask

What does :: mean in C programming?

:: is the scope resolution operator - used to qualify names.

Why do we use call by reference in C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is the purpose of a function call?

A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.

What is the purpose of main () function in C?

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.


1 Answers

To refer to the root namespace. This is often useful if your class or you namespace uses a name which also exists in the root, but at some point you wish to refer to the root version.

For example, if I have overloaded new in my class, but wish at some point to refer to the default (root) new, then I would use ::new to refer to root new.

like image 72
Alex Brown Avatar answered Oct 13 '22 00:10

Alex Brown