简体   繁体   English

将类静态成员定义放入cpp文件 - 技术限制?

[英]Putting class static members definition into cpp file — technical limitation?

one of my "favorite" annoyance when coding in C++ is declaring some static variable in my class and then looking at compilation error about unresolved static variable (in earlier times, I was always scared as hell what does it mean). 我在C ++中编写代码时在我的类中声明一些静态变量,然后查看有关未解析的静态变量的编译错误(在以前的时间里,我总是被吓到了,这是什么意思),这是我最喜欢的烦恼之一。

I mean classic example like: 我的意思是经典的例子:

Test.h Test.h

class Test
{
private:
  static int m_staticVar;
  int m_var;
}

Test.cpp TEST.CPP

int Test::m_staticVar;

What makes it in my eyes even more confusing is the syntax of this definition, you can't use 'static' word here (as static has different meaning when used in cpp, sigh) so you have no idea (except the knowledge static member vars work like that) why on earth there's some int from Test class defined in this way and why m_var isn't. 这个定义的语法让我觉得更令人困惑的是,你不能在这里使用'static'这个词(因为静态在cpp中使用时有不同的含义,叹息)所以你不知道(知识静态成员除外) vars就是这样工作)为什么在地球上有一些来自Test类的int以这种方式定义,为什么m_var不是。

To your knowledge / opinion, why is that? 根据您的知识/意见,为什么? I can think of only one reason and that is making linker life easier -- ie for the same reason why you can't use non-integral constants (SomeClass m_var = something). 我只能想到一个原因,那就是让链接器的生命更容易 - 也就是说,为什么你不能使用非整数常量(SomeClass m_var = something)。 But I don't like an idea of bending language features just because some part of compilation chain would have hard time eating it... 但是我不喜欢弯曲语言功能的想法只是因为编译链的某些部分会很难吃它...

Well, this is just the way it works. 嗯,这就是它的工作方式。 You've only declared the static member in the .h file. 您只在.h文件中声明了静态成员。 The linker needs to be able to find exactly one definition of that static member in the object files it links together. 链接器需要能够在它链接在一起的目标文件中找到该静态成员的一个定义 You can't put the definition in the .h file, that would generate multiple definitions. 您不能将定义放在.h文件中,这将生成多个定义。

UPDATE: C++17 can solve this with an inline variable . 更新:C ++ 17可以使用内联变量解决这个问题。

First, from compiler's point of view, this is perfectly reasonable. 首先,从编译器的角度来看,这是完全合理的。 Why redundant keyword where it is not needed? 为什么冗余关键字不需要?

Second, I'd recommend against static members in C++. 其次,我建议使用C ++中的静态成员。 Before everybody jumps, I will try to explain. 在每个人都跳之前,我会尝试解释。

Well, you are not going to have any public static data members (very rarely useful). 好吧,你不会有任何公共静态数据成员(很少有用)。 In any case, most classes have their own CPP file. 在任何情况下,大多数类都有自己的CPP文件。 If so, a static global, IMO is preferable over a private static member for reasons of dependency reduction. 如果是这样,由于依赖性降低的原因,静态全局IMO优于私有静态成员。 Unlike non-static private data, the static ones are not part of the interface, and there's very little reason why should the user of the h file ever have to recompile, or at all see these members. 与非静态私有数据不同,静态私有数据不是接口的一部分,并且为什么h文件的用户必须重新编译或者根本不需要看到这些成员的原因很少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM