There are two C ++ classes:
Class base {
Public:
Base (int A, int B): M_a (A), m_ B (B ){}
Virtual void func1 ();
Virtual int func2 ();
PRIVATE:
Int M_a, m_ B;
}
Class derived: public base {
Public:
Derived (int A, int B, double D): Base (a, B), m_d (d ){}
Virtual int func2 ();
PRIVATE:
Double m_d;
}
Simulate the implementation mechanism of the C ++ compiler, use the C language to define the base and derived, and implement the Code for creating two classes.
========================================================== ======================================
Typedef void ** vtblptr; // combine the meaning
Struct base_t
{
Vtblptr _ vtbl;
Int M_a;
Int m_ B;
};
Struct derived_t
{
Vtblptr _ vtbl;
Int M_a;
Int m_ B;
Double m_d;
};
New Base
Base_t * pbase = malloc (sizeof (base_t ));
Pbase-> _ vtbl [0] = & _ base_t_func1;
Pbase-> _ vtbl [1] = & _ base_t_func2;
_ Base_t_base (pbase, a, B); // It is said that the constructor does not need this?
New derived
Derived_t * pderived = malloc (sizeof (derived_t ));
Pderived-> _ vtbl [0] = & _ base_t_func1;
Pderived-> _ vtbl [1] = & _ derived_t_func2;
Derived_t Constructor
Void _ derived_t_derived (derived_t * pderived, int A, int D)
{
_ Base_t_base (base_t *) pderived, A, B );
Pderived-> m_d = D;
};