C static keyword vs c ++ private scope?

What is the C ++ equivalent for a translation unit, a local function staticin C?
For example, having the following in bar.c:

static void bar() {
    // ...
}

In C ++, this will be written as a private member function, e.g.

class foo {
    void bar();
};

void foo::bar() {
    // ...
}

The private member function implicitly enters a pointer thisas a parameter, so it is not very similar to a style function static. But even a member function private static bar()will be visible in the public interface (and will remain available to the linker) and will not be comparable.

Although the available scope of these functions is similar, these parameters do not look like good replacements for the style function syntax mentioned static.

Is an equivalent function in an unnamed namespace visible only for the current translation unit?

namespace {
    void bar() {
       // ...
    }
}
-3
1

[C] .

static void bar() { ... }

bar, .

[++]

static void bar() { ... }

bar, .

[++]

namespace {
    void bar() { ... }
}

bar, .

. ++, static. , , .

: ?

C ++ : , . , ++ 2011 3.5 2:

, , , , , , , , :

  • , , , .
  • , , , .
  • , , , .

C 2011 6.2.2. 2:

, , . . .

, , , , .

: , :

2 ++ . bar.cc :

static void bar() {}

main.cc, bar().

extern void bar();

int main() {
    bar();
}

, . bar, main.cc. .

Undefined symbols for architecture x86_64:
  "bar()", referenced from:
      _main in main-c16bef.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
+9

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


All Articles