公告

大中华汽车电子生态圈社区并入开发者社区- 更多资讯点击此

Tip / 登入 to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

XMC实验分享之九十六: C++全局类构造细节

XMC实验分享之九十六: C++全局类构造细节

User12775
Level 5
Level 5
First solution authored First like received

之前发过用C++的Class封装一个LED类型.

class LED
{
    public:    
    static XMC_GPIO_MODE_t InitMode;
    static XMC_GPIO_MODE_t UnInitMode;
    
    static const uint8_t LED_COUNT = 5;
        
        LED(void);
        
        LED(const uint8_t num);
    
        ~LED(void);
        
        void Init(const bool inv_logic);
        void UnInit(void);
    
        void On(void);
        void Off(void);        
        void Toogle(void);
        
    private:
        uint8_t m_num;
        bool m_IsInv;
        bool m_HasInit;
};

用起来也方便, 可以直接声明, 或者使用new操作符来创建:

LED LED1(0);
    LED* pLED4 = new LED(4);

其中第一种方法如果作为一个全局变量, 它的初始化函数会在main函数之前被调用. 这里看看ARMCC编译器调用这个构造函数的细节.

0 点赞
299 次查看
4 评论