从风格的角度来看,这可能会,也可能不会,但风格是主观的。
从技术角度来看,有几个区别:
+----------------+-------------+
| Private Static | File Static |
+----------------------------+----------------+-------------+
| Visible by includers | Yes | No |
+----------------------------+----------------+-------------+
| Accessible to friend | Yes | No |
+----------------------------+----------------+-------------+
| Accessible to all in TU* | No | Yes |
+----------------------------+----------------+-------------+
| Require #include in header | Yes | No |
+----------------------------+----------------+-------------+
*TU: Translation Unit (roughly put: the source file after include resolution)
因此,从技术上讲,static
文件范围内的变量(或匿名命名空间中的变量)可以更加私有,除非它对源文件中跟随它的所有代码可见(这会在一定程度上改变可访问性)。
出于这些客观原因,我个人更喜欢它们。我倾向于让我的标题尽可能地为空,因为它使更改对客户端没有影响更容易(而且我大多数时候都是客户端!)
注意:如果我忘记了差异,请告诉/编辑