吹过的牛逼重视要还的。 talk is cheap show code.
跟群友一起讨论的OOP和C的关系。正好手头有个功能要用到计数器。就写了一个,抛砖引玉,供大家讨论。多谢多谢。
#include <stdio.h>
struct class_counter
{
void(* reset_counter)(struct class_counter *object_counter, unsigned int initial_value);
unsigned int(* tirgger_counter)(struct class_counter *object_counter);
unsigned int(* get_counter_value)(struct class_counter *object_counter);
unsigned int counter_current_value;
};
/* Reset counter to a value*/
void reset_counter(struct class_counter *object_counter, unsigned int initial_value)
{
object_counter->counter_current_value = initial_value;
}
/* trgger counter add 1*/
unsigned int tirgger_counter(struct class_counter *object_counter)
{
object_counter->counter_current_value += 1;
if(0 == object_counter->counter_current_value)
{
return 1;
}
else
{
return 0;
}
}
/* Get value of counter*/
unsigned int get_counter_value(struct class_counter *object_counter)
{
return object_counter->counter_current_value;
}
void main(void)
{
struct class_counter object_test_counter = {
&reset_counter,
&tirgger_counter,
&get_counter_value,
199
};
object_test_counter.reset_counter(&object_test_counter, 1988);
object_test_counter.tirgger_counter(&object_test_counter);
printf("counter value is %d\n", object_test_counter.get_counter_value(&object_test_counter));
}
$ gcc -o counter.exe counter.c
E:\Develop\class_counter>counter.exe
counter value is 1989