码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现C++的类

时间:2018-01-04 11:03:40      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:body   ati   clock   type   c语言实现   简化   static   cloc   实现   

  

typedef struct _s_gpio GPIO;
typedef struct _s_gpio{
    uint32_t gpiox;
    uint32_t gpio_pinx;
    void (*init)(GPIO *gpio);
    void (*toggle)(GPIO *gpio);
}GPIO;

static void _s_gpio_init(GPIO *gpio)
{
	 rcu_periph_clock_enable(LED1_GPIO_CLK);
    gpio_init(gpio->gpiox, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,gpio->gpio_pinx);
}

static void _s_gpio_toggle(GPIO *gpio)
{
    GPIO_OCTL(gpio->gpiox) ^= (uint32_t)gpio->gpio_pinx;
}

GPIO new_outport(uint32_t gpiox,uint32_t gpio_pinx)
{
    GPIO temp;
    temp.gpiox = gpiox;
    temp.gpio_pinx = gpio_pinx;
    temp.init = _s_gpio_init;
    temp.toggle = _s_gpio_toggle;
    temp.init(&temp);
    return temp;
}

	 GPIO led;
	 led = new_outport(GPIOF,GPIO_PIN_6);
    while(1)
    {
        delay_1ms(500);
        led.toggle(&led);
    }

  这样实现C++的类,可以简化代码的重复,也可以让代码结构更晰。

C语言实现C++的类

标签:body   ati   clock   type   c语言实现   简化   static   cloc   实现   

原文地址:https://www.cnblogs.com/mzwhhwj/p/8191343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!