- 论坛徽章:
- 0
|
本帖最后由 xyfree 于 2010-03-26 16:54 编辑
回复 1# tanlijun37
- 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;
- }
复制代码 功能类似的C
- typedef void** VtblPtr; //意思凑合一下吧
- 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时 (tmd chrome假死。。。代码害我重写几次!真没Opera稳定)
- 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 ); // 这句有点疑问,据说构造函数不需要this?
复制代码 new Derived时
- derived_t * pDerived = malloc( sizeof(derived_t) );
- pDerived -> _vtbl[0] = & _base_t_Func1;
- pDerived -> _vtbl[1] = & _derived_t_Func2;
复制代码 derived_t 的构造函数(possibly)
- void _derived_t_Derived( derived_t *pDerived, int a, int d) {
- _base_t_Base( (base_t*)pDerived, a, b);
- pDerived -> m_d = d;
- };
复制代码 |
|