C++ static data members initialization

心已入冬 提交于 2019-12-20 04:58:13

问题


1) Is it true that static data members of classes always get initialized before main() called?

2) Is it true that "static initialization order fiasco" can happen if static data member of class initialization code uses global static variable of other translation unit?

Where can I read more about it? I couldn't find answer in 2003 standard of C++.

Thanks a lot.


回答1:


1) Is it true that static data members of classes always get initialized before main() called?

yes they would always be initialized before program starts executing.

2) Is it true that "static initialization order fiasco" can happen if static data member of class initialization code uses global static variable of other translation unit?

Yes, along with this it also happens for every other thing which is initialized before main() execution.




回答2:


Yes to both answers.

see https://isocpp.org/wiki/faq/ctors#static-init-order for a good description of it.



来源:https://stackoverflow.com/questions/29818985/c-static-data-members-initialization

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