Virtual Function and Inheritance


Derive a class from the base class described in the previous page
class myClassDerived : public myClassBase
{
public:
    int derivedMember;
    int getVal(int x) { return derivedMember + x; }
};

The equivalent C code:
struct myClassDerived
{
    struct myClassBase base;
    int derivedMember;
};
int myClassDerived_getVal(struct myClassDerived *this, int x)
{
    return this->derivedMember + x;
}
struct
{
    long abiVerInfo;
    char *typeName;
    void *baseTypeInfo;
}
myClassDerived_typeInfo =
{
    0, // ABI related stuff.
    "myClassDerived",
    &myClassBase_typeInfo
};
struct
{
    void *typeInfo;
    int (*fun1)(struct myClassDerived *, int);
}
myClassDerived_vtable =
{
    &myClassDerived_typeInfo,
    myClassDerived_getVal
};
void myClassDerived_ctor(struct myClassDerived *this)
{
    myClassBase_ctor(&this->base);
    this->base.vtable = (void*)&myClassDerived_vtable + sizeof(void*);
}

Points to understand

up