Namespaces with external linkage

前端 未结 3 1502
不知归路
不知归路 2020-12-09 23:02

The problem I have is basically the same as \'greentype\' mentions at http://www.cplusplus.com/forum/beginner/12458/

I\'m sharing variables through namespaces and a

3条回答
  •  余生分开走
    2020-12-09 23:54

    The header file should say:

    namespace nn {
        extern int i;
    }
    

    This is a "declaration" not a "definition". You then want a definition in one and only one file:

    namespace nn {
        int i = 1;
    }
    

    Of course, a much better approach is just to not have globals at all.

提交回复
热议问题