简体   繁体   English

在内联函数中使用静态数据成员

[英]using static data members in inline functions

I read the following different sources, 我阅读了以下不同来源,

  1. All the static data members are initialized before main() (even though the main() and static data members exist in different translation unit). 所有静态数据成员都在main()之前初始化(即使main()和静态数据成员存在于不同的转换单元中)。 – Agree –同意
  2. The inline functions are duplicated in each source file or translation unit in which they are invoked. 内联函数在每个调用它们的源文件或转换单元中均重复。 - Agree -同意

It is clearly stated that static data members are initialized before main() then, 明确指出静态数据成员是在main()之前初始化的,

  1. What is the problem, if the inline functions access the static members? 如果内联函数访问静态成员,那是什么问题?
  2. Why it is not safe to use static data members with in inline function? 为什么将静态数据成员与内联函数一起使用并不安全?

Please explain with some clear examples. 请用一些清晰的例子来解释。

There is no problem using static members and inline methods. 使用静态成员和内联方法没有问题。

You can find some similar discussion about static + inline here: static variables in an inlined function 您可以在此处找到有关static + inline的一些类似讨论: 内联函数中的静态变量

Even if it talks about static variables, the mechanism is similar. 即使谈论静态变量,其机制也是相似的。

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

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