What does the syntax :: function_name mean in C ++?

In a function called ::foo(), I don’t understand what the syntax is for. If it was foo::count_all(), then I know what count_alla function of a class or namespace is foo.

In case ::foo()what is the ::link?

+4
source share
4 answers

The operator ::calls namespaceor class. In your case, it calls a global namespace, which is not a named namespace.

, . foo(), , 2 foo s. ::foo().

namespace Hidden {
    int foo();
}

int foo();

using namespace Hidden; // This makes calls to just foo ambiguous.

int main() {
    ::foo(); // Call to the global foo
    hidden::foo(); // Call to the foo in namespace hidden 
}
+4

:: , . :

int foo(); // A global function declaration

int main() {
   ::foo(); // Calling foo from the global namespace.
   ...
+2

, , foo() .:: , foo(), - foo() .

.

void foo()
{
  printf("global foo\n");
}

namespace bar
{
  void foo()
  {
    printf("bar::foo\n");
  }

  void test()
  {
    foo();
    ::foo();
  }
}

bar:: test() :

bar::foo
global foo
+1

:: . . .

fooobar.com/questions/398289 / ...

Below is an example of @CharlesBailey, we are inside the nest namespace. Access to "x" can be changed to the upper namespace depending on whether you are instructed to use the global namespace or not.

namespace layer {
    namespace module {
        int x;
    }
}

namespace nest {
    namespace layer {
        namespace module {
            int x;
        }
    }
    using namespace /*::*/layer::module;
}
0
source

Source: https://habr.com/ru/post/1691214/


All Articles