开发者

How to properly pass the value of a static member variable?

In C++, I have a static member variable in a class.

Then I pass this static member variable to a array 开发者_JS百科of struct initialization. Now my problem is the value of that member in struct is gone.

Please explain if I'm missing some understanding about a static member variable. Did a static member have a limitation of passing its own value?

Please advice.

Many thanks


A static member variable is like a regular global except that:

  • Its name is scoped to that of the class in which it is a member. The class acts like a namespace, but in a more powerful way as it can be used in templates.

  • It can be protected or private in which case only those that have access to the class can access the member.

There is one instance of this, not one per object.

Private static member variables can usually be replaced with a "hidden" variable of the same type in the anonymous names of the compilation unit for the class. This is a preferable option as you then do not need to expose the implementation of your class (which is what private members usually are) in the header.

It would be useful to give an example that duplicates your error so we can see exactly what you are trying to do and why it does not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜