What does “intercom” mean?

The standard says that:

When a name has an internal connection, the object that it designates may be referenced by names from other areas in the same translation unit.

and

A name that has a namespace scope (3.3.6) has an internal relationship if it is a name for a variable, function, or function template that is explicitly declared static;

So, consider the following code:

#include <stdio.h>

namespace A
{
        /* a with internal linkage now.
           Entity denoted by a will be referenced from another scope.
           This will be main() function scope in my case
        */
    static int a=5;
}

int main()
{
        int a; //declaring a for unqualified name lookup rules
    printf("%d\n",a);//-1216872448
}

I really don't understand the definition in the standard. What does it mean:

the object that it denotes can be called by names from other areas in the same translation unit.

+4
source share
3 answers

A #include d .

A name , , ( ). static, , , .

a, a, name a main . main a, a a. a , a, main. a a main, cout<<A::a using namespace A; , main.

+5

" " - , . .cpp , .

, . ; "" , . "" , .

, printf, ( ) printf . 1), printf , 2), , , 1) 2).

printf - ; , . , - . , main A::a, static , , , A::a. , A::a .

, , , a, main , , , . . main :

int main()
{
    printf("%d\n", A::a);
}

5.

+2

, , .

, .

a. a, A. , , , .

, .

static int a = 5; // a new entity with name `a` that has internal linkage.

int main()
{
    int a; // This is a new entity local to function main with no linkage.
           // It isn't initialized, so you have undefined behavior if you try to
           // access it.
}

, a, , .

, , :

static int a = 5; // A new entity with the name `a` that has internal linkage.

void f()
{
   extern int a; // This is a new declaration for the same entity called `a` in
                 // the global scope.
}

, . .

. f() f(), , , f a , . , a static, a .

, , .

0

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


All Articles