Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. /* Instead of Object*/
  4. typedef struct {
  5.   int x;
  6. }Base;
  7. #define initBase {1}
  8.  
  9. /* Instead of any of our classes that include a field of type Object*/
  10. typedef struct{
  11.   Base b;
  12.   int y;
  13. }Extension;
  14. #define initExtension(z) {initBase,z}
  15.  
  16. /* Instead of sync that uses a pointer to Object */
  17. int doubleBase(Base *b){
  18.   return 2*b->x;
  19. }
  20.  
  21. /* Instead of SYNC that uses a pointer to some class that extends Object */
  22. #define DOUBLEBASE(a) doubleBase((Base *)a)
  23.  
  24. /* Here we see how the type conversion works */
  25. int main(int argc, char* args[]){
  26.  
  27.   Extension e = initExtension(38);
  28.  
  29.   /* here you see the 2 level de-referencing e.b.x*/
  30.   printf("%d %d\n",e.b.x,e.y);
  31.  
  32.   /* here you see the typecast at work! */
  33.   printf("%d\n",DOUBLEBASE(&e));
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement