c++ static array declared in h file gives warning 'defined but not used'

前端 未结 2 1302
时光取名叫无心
时光取名叫无心 2021-02-15 10:31

I\'m curious about the following. I have a simple C array declared in a header file like this:

static int userCardsIndexes[INITIAL_CARDS_NUMBER] = {0, 1, 8, 9, 1         


        
2条回答
  •  有刺的猬
    2021-02-15 11:25

    Static variables are local to the translation unit they are defined in. When you do that in a header, you get a separate copy in each cpp file you include it in. Probably not what you wanted. The compiler obviously notices that some of these copies are not used at all.

    When you add const you have a different situation. In C++ a const object at file scope is also static by default. So const and static const mean the same thing.

    The constant array will also have a copy in each cpp file, but that doesn't matter much as it will always have the same value anyway.

提交回复
热议问题