static class member of class's own type [duplicate]

此生再无相见时 提交于 2019-11-29 07:14:38

The reason for class can't have data members of its own type is the compiler must know the size of class object. For example, one class is a local variable in function, the compiler can handle the stack only it knows the class size.

For your case, the static class member doesn't reside in class object, so has no impact to size of class object. It's OK.

Formally, the distinction is that the declaration of a static member in a class is not a definition. You must provide a definition elsewhere (exactly once), and the compiler doesn't need to know the size until it encounters the definition. Static members do not impact on the size of the class itself. (In many ways, the static member declaration in the class is very much like an extern non-member declaration.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!