Why "extern int & c"? work fine?

In C ++, the reference variable must be initialized. int & a; // Error

static int &b; // Error

But

extern int &c; // No error

Why does the compiler not give an error for the externspecifier reference ?

+6
source share
3 answers

The keyword externis a compiler directive in which you now declare a character to be filled during linking, taken from another object file. Initialization is EXPECTED when the actual character is determined.

If you have ac file with

int foo;
int &bar = foo;

And bc file with

extern int &bar;

b.c b.o, bar . bar a.o b.o bar a.o

- ( ).

+12

extern?

extern int &c; , . , c - .

cppreference " " extern .

+5

8.3.2
5 [...] (8.6.3), , extern (7.1.1), (9.2 ) (8.3.5); . 3.1.

. , . , ( ) .

No one forbids you to include an initializer in a link declaration with an explicit keyword extern. However, as usual, it will turn a non-defining declaration into a definition.

+5
source

All Articles