Under what circumstances can an extern variable be used in definition?

后端 未结 6 1218
情书的邮戳
情书的邮戳 2020-12-21 12:16

I am very very sorry. I didn\'t know my incomplete code attachment would create such a mess. I am very glad to see so many sincere helps.

This code will compile:

6条回答
  •  暖寄归人
    2020-12-21 12:41

    All variable declarations with an initializer are also definitions; that's an overriding rule. Regardless of extern. There are even cases where you need an extern on a definition: you can only instantiate a template using a variable which has external linkage. And const variables have internal linkage by default, so you need something like:

    extern int const i = 42;
    

    if you want to use it to instantiate a template.

提交回复
热议问题