fork download
  1. #include <stdio.h>
  2. struct Base {
  3. int num;
  4. #define DEFAULT_BASE() {.num =1}
  5. };
  6.  
  7. struct DerivedA{
  8. struct Base base;
  9. int a;
  10. #define DEFAULT_DERIVEDA() {DEFAULT_BASE() , .a = 2}
  11. };
  12.  
  13. struct DerivedB{
  14. struct Base base;
  15. int b;
  16. #define DEFAULT_DERIVEDB() {DEFAULT_BASE() , .b = 2}
  17. };
  18.  
  19. struct DerivedA instA = DEFAULT_DERIVEDA();
  20. struct DerivedB instB = { DEFAULT_BASE() , .b = 3 };
  21.  
  22. void func(struct Base * p){
  23. p->num++;
  24. }
  25.  
  26. void Base_Print(struct Base * obj){
  27. printf("num:%d " , obj->num);
  28. }
  29.  
  30. void DeriveA_Print(struct DerivedA * obj){
  31. printf("A->");
  32. Base_Print(obj);
  33. printf(", a:%d\n" , obj->a);
  34. }
  35.  
  36. void DeriveB_Print(struct DerivedB * obj){
  37. printf("B->");
  38. Base_Print(obj);
  39. printf(", b:%d\n" , obj->b);
  40. }
  41.  
  42.  
  43. int main() {
  44. func(&instA);
  45. func(&instB);
  46. DeriveA_Print(&instA);
  47. DeriveB_Print(&instB);
  48. //printf("A->num:%d , a:%d\nB->num:%d , b:%d" , instA.base.num , instA.a , instB.base.num , instB.b );
  49. }
Success #stdin #stdout 0s 4480KB
stdin
Standard input is empty
stdout
A->num:2 , a:2
B->num:2 , b:3