user-0 提问: OOC实现多重继承

回复:
user: user-0
content: mLee79 ,你的程序在C99下编译不通过,intel0011,感谢分享,正在研究lw_oopc

user: user-1
content: C语言又没有继承

user: user-2
content: 无聊的写一个， 大部分 C/C++ 编译器能兼容内存布局，  最好不要在接口里定义成员变量， 只留纯虚函数， 做接口继承 ....  $ for src in foobar.h foobar.c main.cpp; do echo ------ $src -------; cat $src; done;  gcc -w foobar.c main.cpp -o t -lstdc++; echo -------------------------------------- ;./t ------ foobar.h ------- #ifndef __INC__FOOBAR__H__ #define __INC__FOOBAR__H__  #include <inttypes.h>  #ifdef _MSC_VER #define FOOCALLTYPE             __fastcall #endif  #if !defined(FOOCALLTYPE) && defined(__i386__) #define FOOCALLTYPE             __attribute__((fastcall)) #endif /* FOOCALLTYPE */  #ifndef FOOCALLTYPE #define FOOCALLTYPE #endif /* FOOCALLTYPE */  #ifdef __cplusplus class A { public:         virtual int FOOCALLTYPE getA() const { return a; }         virtual int FOOCALLTYPE getB() const { return b; }         virtual void FOOCALLTYPE setAB(int a, int b) { this->a = a; this->b = b; } protected:         int a, b; }; class A2 : public A { public:         virtual void FOOCALLTYPE show() const = 0; }; class B { public:         virtual int FOOCALLTYPE getC() const { return c; }         virtual int FOOCALLTYPE getD() const { return d; }         virtual void FOOCALLTYPE setCD(int c, int d) { this->c = c; this->d = d; } protected:         int c, d; };  class AB : public A2, public B { public:         virtual void FOOCALLTYPE show() const {                 printf("class AB, %d %d %d %d\n", a, b, c, d);         } }; #else typedef struct A_t A; typedef struct A2_t A2; typedef struct B_t B; typedef struct AB_t AB; struct A_vtbl_t {         int (FOOCALLTYPE*getA)(const void* thiz);         int (FOOCALLTYPE*getB)(const void* thiz);         void (FOOCALLTYPE*setAB)(void* thiz, int a, int b); }; struct A2_vtbl_t {         struct A_vtbl_t base___;         void (FOOCALLTYPE*show)(const void* thiz); };  struct B_vtbl_t {         int (FOOCALLTYPE*getC)(const void* thiz);         int (FOOCALLTYPE*getD)(const void* thiz);         void (FOOCALLTYPE*setCD)(void* thiz, int c, int d); };  struct A_t {         const struct A_vtbl_t* vtbl;         int a, b; }; struct A2_t {         const struct A2_vtbl_t* vtbl;         int a, b; }; struct B_t {         const struct B_vtbl_t* vtbl;         int c, d; }; struct AB_t {         A2      cA;         B       cB; }; #endif  #ifdef __cplusplus extern "C" { #endif /* */  AB*  newAB(int a, int b, int c, int d); void freeAB(AB* o);  void test(const A* oA, const B* oB);  AB*  newABpp(int a, int b, int c, int d); void freeABpp(AB* o);    #ifdef __cplusplus }; #endif /* */  #endif /* __INC__FOOBAR__H__ */ ------ foobar.c ------- #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include "foobar.h"  int FOOCALLTYPE fGetA(const A* thiz){   return thiz->a; } int FOOCALLTYPE fGetB(const A* thiz){   return thiz->b; } void FOOCALLTYPE setAB(A* thiz, int a, int b) { thiz->a = a; thiz->b = b; } void FOOCALLTYPE fShow(const A2* thiz) {         const AB* o = (const AB*)((uint8_t*)thiz - offsetof(AB, cA));         printf("fShow, %d %d %d %d\n", o->cA.a, o->cA.b, o->cB.c, o->cB.d); } int FOOCALLTYPE fGetC(const B* thiz){   return thiz->c; } int FOOCALLTYPE fGetD(const B* thiz){   return thiz->d; } void FOOCALLTYPE setCD(B* thiz, int c, int d) { thiz->c = c; thiz->d = d; }  const struct A2_vtbl_t vtblA = { {fGetA, fGetB, setAB}, fShow }; const struct B_vtbl_t vtblB = { fGetC, fGetD, setCD }; AB*  newAB(int a, int b, int c, int d) {         AB * o = (AB*)malloc(sizeof(AB));         o->cA.vtbl = &vtblA;         o->cB.vtbl = &vtblB;         o->cA.a = a;         o->cA.b = b;         o->cB.c = c;         o->cB.d = d;         return o; }  void freeAB(AB* o) {         free(o); }  void test(const A* oA, const B* oB) {         const AB* ab1 = (const AB*)((uint8_t*)oA - offsetof(AB, cA));         const AB* ab2 = (const AB*)((uint8_t*)oB - offsetof(AB, cB));          printf(" %d, %d, %d, %d\n", oA->vtbl->getA(oA), oA->vtbl->getB(oA), oB->vtbl->getC(oB), oB->vtbl->getD(oB));         printf(" AB1: %d, %d, %d, %d\n", ab1->cA.a, ab1->cA.b, ab1->cB.c, ab1->cB.d);         printf(" AB2: %d, %d, %d, %d\n", ab2->cA.a, ab2->cA.b, ab2->cB.c, ab2->cB.d); } ------ main.cpp ------- #include <stdio.h> #include "foobar.h"  AB*  newABpp(int a, int b, int c, int d) {         AB* o = new AB;          o->setAB(a, b);         o->setCD(c, d);         return o; } void freeABpp(AB* o) {         delete o; }  int main() {         AB* a1 = newAB(1,2,3,4), *a2 = newABpp(10, 20, 30, 40);          test(a1, a2);         test(a2, a1);          printf("%d %d %d %d\n", a1->getA(), a1->getB(), a1->getC(), a1->getD());         printf("%d %d %d %d\n", a2->getA(), a2->getB(), a2->getC(), a2->getD());          a1->show();         a2->show();          return 0; }  --------------------------------------  1, 2, 30, 40  AB1: 1, 2, 3, 4  AB2: 10, 20, 30, 40  10, 20, 3, 4  AB1: 10, 20, 30, 40  AB2: 1, 2, 3, 4 1 2 3 4 10 20 30 40 fShow, 1 2 3 4 class AB, 10 20 30 40

user: user-3
content: 不是很理解你的意思，建议参考lw_oopc（国产)  JCOOP sooc OOOACSources OOC-Embeded cos-0.8

user: user-4
content: 看了半天才反应过来。。。这个不是结构体么。。。。

user: user-2
content: 用 offsetof 调整呗， 最好只弄 接口继承， 不然很难 ABI 兼容 ...