0

Suppose I have a header file : header.h

const int i =10;

Multiple source files can have access to this variable "i" just by including this header file. Since it is a constant, none of the source file can change the definition. So what will happen if I change header.h like this:

extern const int i;

And define i in any of the other .cpp file.

In other words, how does it matter if "i" has internal(every source file has its own definition) or external(definition is shared) linkage because in any case definition of "i" will never change.What is the advantage of making it extern?

Sloth
  • 19
  • 2
  • I'm getting a "duplicate definition" link error if I put the second definition in >1 TU. – HolyBlackCat Apr 14 '22 at 21:35
  • Interesting side-reading: [Use case for initialized extern variable](https://stackoverflow.com/questions/32872607/use-case-for-initialized-extern-variable) – user4581301 Apr 14 '22 at 21:59
  • Why didn't you try that situation first before coming here to ask? You would have seen the error that HolyBlackCat saw. – Eljay Apr 14 '22 at 23:01
  • @HolyBlackCat My mistake. I edited my question. I hope my question is clear now. – Sloth Apr 15 '22 at 13:13
  • `const` just tells the compiler it is not mutable. It's not related to `extern` – JHBonarius Apr 15 '22 at 13:19

0 Answers0