cpp_noname
cpp_noname

Reputation: 2071

Namespace Syntax "::Foo"

What does this syntax mean when there is no namespace attached in front of the double colons "::" ?

Suppose I have a class Foo. And somewhere in the code there is something like ::Foo.

Upvotes: 1

Views: 75

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

It denotes the global namespace.

Consider this code for example

#include <iostream>

int x = 10;

namespace N
{
    int x = 20; // Or you could write int x = 10 + ::x;
}

int main()
{
    int x = 30;

    std::cout << ::x + N::x + x << std::endl;
} 

Take into account that in this statement

    std::cout << ::x + N::x + x << std::endl;

you could write ::N::x instead of N::x because namespace N is enclosed in the global namespace.

Upvotes: 3

Related Questions