-1

what is use of having static variables defined in a header file . Each source file include the header file will get its own copy . what is the use of this ?

This questions was asked in one of the interview for me. He stressed me to give the use.He said they are using this in their code.

2 Answers2

3

It's quite possibly an error, exactly for the reason that you are pointing out. On the other hand, you will sometimes see tiny static functions in a header, where the programmer either didn't care that the code would be duplicated, or hoped that the code would be removed by the linker or inlined.

Since it is unusual and suspicious, the programmer should have written a comment why this is being done if they had a good reason. No comment makes it more likely that there was no good reason.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

If in header one include rule is obeyed then static - means not for linkage (is analog of anonymous namespace in C++)

spin_eight
  • 3,925
  • 10
  • 39
  • 61