RT-Thread C++初始化全局对象的时机 #32
Answered
by
mysterywolf
mysterywolf
asked this question in
Quick Reference Handbook
-
原题目:警惕C++全局变量初始化目前风险已解除,无需警惕,原因请参考答案构造函数里初始化设备这种行为可能会引发一些行为不可控。因为构造函数什么时候初始化并不确定,例如,一旦传感器通过构造函数初始化的时机比IIc设备框架要早,那么,传感器肯定是不工作的。例如: https://github.com/RTduino/RTduino/blob/master/core/HardwareSerial.cpp#L20 目前没有发生上述描述的情况,即便是全局对象的构造函数也可以正确获取到设备,希望是杞人忧天,对此保持关注即可,无需做调整 |
Beta Was this translation helpful? Give feedback.
Answered by
mysterywolf
Sep 26, 2022
Replies: 1 comment
-
|
经过确认: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mysterywolf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
经过确认:
https://github.com/RT-Thread/rt-thread/blob/master/components/libc/cplusplus/cxx_crt_init.c#L68
全局对象的实例化,是在component级别进行初始化的。风险解除。