I know C programming can implement OOP concept, and I saw a small code do that like bellow. However the implementation in main function confuse me:
Car c;
vehicleStart((Vehicle*) &c);
Why struct Car c can directly pass to vehicleStart parameter, although it has been type casting. I think they have different type in struct
Vehicle base;
Object base;
so this operation confuse me.
#include <stdio.h>
typedef struct Object Object; //!< Object type
typedef struct Vehicle Vehicle; //!< Vehicle type
typedef struct Car Car; //!< Car type
typedef struct Truck Truck; //!< Truck type
/*!
* Base object class.
*/
struct Object {
int ref; //!< \private Reference count.
};
static Object * objRef(Object *obj);
static Object * objUnref(Object *obj);
struct Vehicle {
Object base; //!< \protected Base class.
};
void vehicleStart(Vehicle *obj);
void vehicleStop(Vehicle *obj);
struct Car {
Vehicle base; //!< \protected Base class.
};
struct Truck {
Vehicle base; //!< \protected Base class.
};
/* implementation */
void vehicleStart(Vehicle *obj)
{
if (obj) printf("%x derived from %x\n", obj, obj->base);
}
int main(void)
{
Car c;
vehicleStart((Vehicle*) &c);
}